Basic PHP cURL Example

Using PHP's cURL Functions, it's easy to grab data from another website and manipulate it to meet your needs. Here's a quick example that returns the output of http://www.example.com/ to the $content variable.

$url = 'http://www.example.com/'; //specify the URL to connect to

$ch = curl_init(); //start cURL session
curl_setopt($ch, CURLOPT_URL, $url); //specify the URL to connect to
curl_setopt($ch, CURLOPT_HEADER, false); //don't return the header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //store the output as a variable
$content = curl_exec($ch); //go and fetch the webpage
curl_close($ch); //close the cURL session

echo htmlspecialchars($content); //print out the html you just grabbed

Of course, once you have the page, there are plenty of things more exciting than echoing the code that you can do with it. For example, with a little RegEx help, you can pull meaningful data out and store it for future use.


Comments

Loading…

This post was published on September 3rd, 2014 by Robert James Reese in the following categories: cURL and PHP. Before using any of the code or other content in this post, you must read and agree to our terms of use.