How to Use cURL PHP Page Contents Strings
A quick way to get the contents of a Web page into a string variable in PHP is to use the "file_get_contents" function. However, there are many Web servers that don't allow Web page contents to be captured this way. In those cases, you need to use the PHP Client URL, or cURL, library to retrieve the page contents and place it in one or more PHP strings. Even though cURL is an external library to PHP, it is typically installed as part of a "standard" PHP installation.
Instructions
-
-
1
Review the documentation for the cURL library. Pay special attention to the "curl_setopt" function, which contains a list of the many possible options you can set using cURL. Identify the options you need to set in order to retrieve data given the source of the data and the string or strings into which you will insert the data.
-
2
Create a function to take a URL as a parameter, and return the contents of that URL. Set a variable to initialize cURL. For example, type:
function get_web_page($url) {
$ch = curl_init(); -
-
3
Use the "curl_setopt" function to set each of the cURL options. Use "1" for "on" and "0" for "off." Set the URL from which you are going to extract data, set the option to return from capturing the URL with the data saved in a variable and set the maximum amount of time to wait if the page does not load. For example, type:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); -
4
Set a user agent string to have cURL impersonate a browser, set the option to automatically follow redirects, and set the option to fail on an error. For example, type:
curl_setopt($ch, 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); -
5
Call "curl_exec" to fetch the data into a string variable. For example, type:
$webpage = curl_exec($ch);
-
6
Check to see if there was an error retrieving the page and then close the curl transaction. For example, type:
if (curl_errno($ch)) return false;
curl_close($ch);
return $webpage;
}
-
1