Using cURL With PHP To Fetch A Webpage
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//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.
1 2 |
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.
1 |
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.
2 Comments
Yogie
It works like a charm. Thank you so much.
Paul Robinson
Glad it helped. 🙂