Home > Tutorials > PHP > Using cURL With PHP To Fetch A Webpage
Permalink to Using cURL With PHP To Fetch A Webpage

Using cURL With PHP To Fetch A Webpage

by on 02.08.2010 | no comments

With APIs such as Twitter’s becoming more & more popular connecting to a webservice or webpage in a script is now a common request. As such here is how to fetch a webpage via PHP using cURL.

To fetch a webpage or results from a webservice such as an API using cURL is actually simpler and most people think. Let’s get straight into it.

//initialize a new curl resource
$ch = curl_init();
//Fetch the timeline
curl_setopt($ch, CURLOPT_URL, 'https://example.com/');
//send data via $_GET
curl_setopt($ch, CURLOPT_GET, 1);
//do not return the header information
curl_setopt($ch, CURLOPT_HEADER, 0);

	//If authentication is needed
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	//Set user and pass
	curl_setopt($ch, CURLOPT_USERPWD, "user:pass");

//If SSL verification is needed. Delete if not needed
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
//Give me the data back as a string... Don't echo it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Warp 9, Engage!
$content = curl_exec($ch);
//Close CURL connection & free the used memory.
curl_close($ch);

The code here is actually more complicated than normal. However I thought I would give a few extra cURL features that generally aren’t covered.

Let’s go through a few of the more complicated lines and explain what they do.

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");

Should the server you are connecting to require a login, you can use these lines to set the username & password. If you do not need to login to the server, you can delete them.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);

If you are connecting to a https:// connection you can choose whether to verify the SSL certificate of the server. Again if you do not need this or you aren't connecting to a https:// connection you can delete this line.

The rest is hopefully explained by the comments in the code above. Data from the cURL request will be stored in $content to be used however you wish. If you have any questions, feel free to drop me a comment.

TAGS:

Written by Paul Robinson

A Web coder in languages such as CSS, X/HTML, jQuery, but mostly PHP. Addicted to Girls Aloud, Jennifer Morrison, Carah Faye Charnow, TV Show Chuck, and completely in love with Yvonne Strahovski's smile.

Give something back!

If you LOVED this tutorial and would like to show your appreciation, please consider or a little something from our Amazon Wishlist.

Leave a comment

Please enclose code in [lang] tags. For example [php] echo 'hello world'; [/php]

* Name, Email, Comment are Required