There are some definitional misunderstandings when developers say cUrl in PHP, or libcurl in PHP. But most of the time, they mean PHP/CURL.
In this tutorial, we will first find out what cUrl, libcurl and PHP/CURL are, because we want to make everyone know the differences in these three concepts. Then we will move on to use finish a task using PHP/cURL.
The task we will accomplish at the end of this tutorial is to fetch content of a google page by passing a search term "curl". And we will also provide some reference how you can extend this tutorial to go further.
Firstly let us understand the concepts of curl, libcurl and PHP/cURL.
Now you have an idea of the different terms. For "PHP/cURL", most of developers also refer it to "curl in PHP", "curl with PHP" and so on. In this tutorial, we will call it "curl in PHP" to follow the common term. The goal of the task in this tutorial is to use curl in PHP to fetch data from Google, and the searching term which will be sent to Google is "curl". Let us start the task!
To use curl in PHP is very straightforward. The basic idea of using curl in PHP is
To complete our goal, the code is simple as
<?php
//step1
$cSession = curl_init();
//step2
curl_setopt($cSession,CURLOPT_URL,"http://www.google.com/search?q=curl");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
//step3
$result=curl_exec($cSession);
//step4
curl_close($cSession);
//step5
echo $result;
?>
Clearly there are more tasks you can do with curl in PHP, this tutorial is almost the simplest way to use it. However you can expand it to perform more tasks such as authentication to a password protected account and fetch data from there. Areas you may need to look further to perform more advanced tasks are listed below:
Thank you for reading this article, and if you have any encountered anything different, have a different solution or think our solution is wrong, do let us know in the comment section. We will be very happy to hear that.
If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.