A Dragon in Sheep’s Clothing

cURL in PHP (update)

January 18, 2007 · 2 Comments

Ironically, today I discovered the PHP manual I wish I’d found back when I first tried learning about cURL: the Practical PHP Programming wiki written mosly by Paul Hudson.  Nice, easy-to-understand cURL and PHP explanations. And wouldn’t you know it, Paul Hudson wrote the O’Reilly book PHP in a Nutshell. Where was this search result 6 weeks ago?! ;-)

Well, I did promise an update, so I’ll post a brief one based on the code I posted yesterday. Remember, I was having trouble understanding how to use cURL instead of the PHP include function, so that’s all that this covers. To learn about using cURL in other ways, head over to Paul Hudson’s wiki. :-)

(Note: I realized that I could shorten it by one line (the timeout variable) so I did for this example.) 

Here’s the cURL code altogether:
<?php
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://www.host.com/remotefile.html');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
 if (isset($_SERVER['QUERY_STRING']))
  $url .= "&".$_SERVER['QUERY_STRING'];
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>

Now let’s break it apart.

You open with a standard PHP tag:
<?php

Then you set some variables.

This is the basic way to start a cURL query:
$ch = curl_init();

Next, set the options that the query will use.

This one identifies the remote file (the URL) you want to access:
curl_setopt ($ch, CURLOPT_URL, 'http://www.host.com/remotefile.html');

This one tells cURL not to immediately print the file that it’s querying, but to keep the results until we call for them later:
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

And this one tells cURL how long it can take (in seconds) to connect to the remote file. I chose 5 seconds (if it can’t connect in 5 seconds, it will stop trying and the rest of my page will still load):
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);

Here’s where an if-else statement with extra variables can go (this is standard PHP):
if (isset($_SERVER['QUERY_STRING']))
$url .= "&".$_SERVER['QUERY_STRING'];

Set a variable (in this case, file_contents) that you can call in the future to execute the cURL query based on the items you identified above:
$file_contents = curl_exec($ch);

By using a variable you can call later, the results can be displayed in the place and format of your choice. 

End the cURL session (this ends the process so that server memory is freed up):
curl_close($ch);

This PHP code calls the variable we set above to execute the cURL query, and displays the results:
echo $file_contents;

End your PHP like normal:
?>

As mentioned above, by calling a variable to execute the cURL query, you can manipulate and format the results. For example, use $echo to add text before or after the results from the cURL query, wrap it in a table, change font colors and so on and so on. For details on how to format echo strings, visit the official PHP web site.

Categories: Blogs, podcasts and rss, oh my! · Hosting · PHP

2 responses so far ↓

You must be logged in to post a comment.