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.
1. $url = 'http://www.example.com/'; //specify the URL to connect to 2. 3. $ch = curl_init(); //start cURL session 4. curl_setopt($ch, CURLOPT_URL, $url); //specify the URL to connect to 5. curl_setopt($ch, CURLOPT_HEADER, false); //don't return the header 6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //store the output as a variable 7. $content = curl_exec($ch); //go and fetch the webpage 8. curl_close($ch); //close the cURL session 9. 10. 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.
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 & Conditions.