cURL in PHP

Okay, I feel pretty good. (I always do when I solve a snarly code problem.)

A few weeks ago, I loaded some PHP pages on a site I have hosted at Dreamhost, and they used the include() code to bring in results from remote files.

However, Dreamhost does not allow the include code (or fopen, or fsockopen) for remote files because of “security issues.” They suggest saving the files to my local site (impossible, the files weren’t available to me) or using cURL to get the remote file or the results from processing the remote file.

But when I searched for help on cURL, I seemed to find mostly manual-type pages with far more definitions than I needed and too technical of an explanation for someone who only dabbles in manipulating PHP. The Dreamhost wiki only explained a little — not enough for someone who hasn’t used cURL before and has a history of screwing up stuff. ;-)  (Plus, I swear, I could not wrap my head around it the first 8 times I looked at that page. Now I know what they’re talking about, but that’s after gleaning info from several searches.) The Dreamhost page displays the cURL code for “Fetching a web page” without giving someone like me a clue that this is the include/fopen thingy. Oy.

Anyway. That was a while ago. Last night I tried again, and this time found a couple of gems in the search results. With the help of those 2 or 3 pages, and some trial and error, I was finally able to code the cURL I needed.

So for anyone else out there who needs cURL help, I offer up a couple of “copy and paste” examples of replacing include() code with cURL:

To replace this simple include:
<?php include "http://www.host.com/remotefile.html"; ?>

Use this cURL code:
<?php
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.host.com/remotefile.html');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
// display file
echo $file_contents;
?>

To replace an include code with an if statement:
<?php
 $url = "http://www.host.com/remotefile.html";
 if (isset($_SERVER['QUERY_STRING']))
  $url .= "&".$_SERVER['QUERY_STRING'];
 include $url;
?>

Use this cURL code:
<?php
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.host.com/remotefile.html');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 if (isset($_SERVER['QUERY_STRING']))
  $url .= "&".$_SERVER['QUERY_STRING'];
$file_contents = curl_exec($ch);
curl_close($ch);
// display file
echo $file_contents;
?>

More resources:
http://www.phpit.net/article/using-curl-php/

Forums that helped me:
http://www.feeddigest.com/forums/3_1228_0.html
http://www.feeddigest.com/forums/3_1176_0.html

This evening, I’ll add on a quick breakdown of the parts of the cURL code in these examples. One way to learn code is to get the code you need and then ask “why this?” :-)

Update has been posted. :-)

3 responses to “cURL in PHP

  1. Pingback: cURL in PHP « Cyberspace Nova

  2. I didn’t know you could include a remote file in PHP EVER!!! I am surprised to even find this post…. Ya CURL is very useful for server reloads

  3. Pingback: Using cURL instead of fopen() in XSLTransform » free icons download

Leave a comment