PDA

View Full Version : Loading specific HTML into PHP


YourDirector
07-12-2011, 11:44 AM
Hey everyone,

I'm building a website in PHP where the header and footer are built into the index.php file, inbetween them is a cell in which all the other content for the website is loaded from external files.

I currently do this by adding:
<?
echo $content
?>
into the aforementioned cell, then before the head of the index page having this for each page on the site:

if (isset($HTTP_GET_VARS["home"])){
$content = file_get_contents("data/home.dat");
}

To call this I simply have my hyperlinks set to "/?home" etc.
While this works, with a large site you can imagine the list of above commands gets pretty weighty. I know this is a really terrible way to do this but I don't know any other way.

Can anyone point me towards a better way of loading external html content into my PHP page, variably dependant on the link just clicked by the user?

Thanks

davidj
07-14-2011, 04:42 PM
cURL is the best way.....

function getPage( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);

$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );

$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}