Friday, December 5, 2008

Returning Content to PHP Variable with cURL

I have a few examples of using cURL here in PHP but this parameter allows you to store the content of the selected source to a PHP variable.

Say, for example, you are loading up a page with cURL and want that information store in a PHP variable to use throughout your site.  You can do this by adding the following code to your cURL request:

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // return the page content

So here is a full block of code:

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'URL_TO_THE_PAGE');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$savedvalue = curl_exec($curl_handle);
curl_close($curl_handle);

In this case, $savedvalue will hold all the information returned by that page request.

cURL is very powerful and worth learning about.  Here is a good resource: PHP login/download file - CURL libraries.

No comments: