Grab HTML page as String Value using PHP cURL
I have been hanging around the DevNetwork for the last week and I have seen so many people have issues with their connections using cURL in PHP.
So I thought why not provide the basic connection script that uses cURL function to connect to the website url in question, extract the page execution output and save that content within a string variable.
Example issues some coding noobs are experiencing:
there is a web page with a table that has a changing number in it on Site A. I want to use php to grab that number and display it on my site, place it in a form, store it to a DB and then do various calculations as the number of entries grow.i want this to happen automatically. so once i open this php page, it pulls the data, and stores it to my mysql db every x intervals of time
and many more which I am shocked that not one person reads the PHP Manual first:
I have a one question. How can I get value from another site or I send a POST to another site?
So here is the basic layout for a cURL connection:
<?php // sets url value $url = "http://example.com/index.php"; // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); //echo $output; ?>
This will basically connect to http://example.com/index.php and load that page into the string $output. When you have the content of your HTML page within the string $output you can then search that ($)string for the required content.
This is only a basic markup of php code to extract the html contents of a given page. If you need to grab content from a page that requires you to login first then you need to add certain items before you can do that such as cookies and headers.
In otherwords you can get this cURL code to log you in to the site, grab the content and log you back out again. cURL is a very powerful feature of PHP coding to display content from one website address on your own site. I use cURL on a regular basis and have completed many projects that require this function to be successful.
One good example of this would be ‘Company A’ has accounts with ‘Supplier A’, ‘Supplier B’ and ‘Supplier C’. ‘Company A’ have to keep logging in to each individual suppliers account to check the price of their products. The use of cURL allowed me to script a code connecting to each account and grabbing the price for each item they required and displaying it on the screen for them to see without even having to login once manually.
For a more detailed explanation of cURL check out: cURL the manual.






Shaun is a man with a background in joinery, music, administration, management, web development and technologies. A devoted friend and father to his wonderful daughter, he is also a family man. He likes to create and produce things which has led him into a stronger, more passionate pursuit of development.



danny | August 21st, 2011 at 5:02 pm #
great advice thanks for sharing the code makes it really simple with your comments too was wondering how you would code if you had to login first etc
great post
Shaun Ellerton | August 21st, 2011 at 5:04 pm #
Thanks Danny for your comments. I will probably produce another article including how to connect via cURL if login is required.
That will be sometime this week probably.
Thanks again Danny