views:

107

answers:

4

I'm using the MVC framework CodeIgniter for my work, if it's relevant.

In one of my pages/controllers, I want to capture the HTML outputted by another page on the site as a string (to use as an email or newsletter). I tried:

$string = file_get_contents('http://www.examplesite.com/path/to/page');

But $string just becomes false, which the documentation says means it failed. I must be missing something tragic...

A: 

Maybe you can try using cURL. You can see an example here: http://www.php.net/manual/en/curl.examples-basic.php

dusan
A: 

fopen_wrappers is probably disabled on your server. Try using Curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_URL,'http://www.examplesite.com/path/to/page');
$html = curl_exec($ch);
Galen
Curiously, this returns the "Apache 2 Test Page". Any ideas?
Steven Xu
If it helps, the error in my PHP logs is a 404.
Steven Xu
make sure you have the correct url passed to CURLOPT_URL
Galen
A: 

I've got a slightly better solution that at least works. CodeIgniter allows a third argument for the call to the view so that I can do this:

$string = $this->load->view('path/to/view', '', true);

This still doesn't resolve my initial concern of duplicating the calls in the controller, but at least it's better. Note that this DOES still require me to know exactly which controller is being used.

Steven Xu
A: 

You seem to have answered your own question. You might consider doing the following which will let you load an arbitrary page:

/* index method inside example controller */
function index($url) {
    $content = $this->load->view(urldecode($url), '', true);
}

// requesting this page generates string output for a specified url
$url = urlencode('/relative/path/to/view/');
http://www.example.com/index.php/example/index/ . $url;
cballou
Unless you're seeing something I'm not, loading the view is not good enough - the view gets passed data from the controller, whose function calls are different for every controller.
Steven Xu
When you pass 'true' as the thrid variable, the view gets sent as a string, not rendered. At least that's what the CI docs say.
Zack
I believe @Steven's gripe with this is he wants to pass dynamic data to the view from the controller. It's alot more work than can easily be solved in SO without more details.
cballou