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.
We have published a follow-up tutorial at From PHP/cURL to Guzzle.
Table Of Content
Firstly let us understand the concepts of curl, libcurl and PHP/cURL.
- curl: A command line tool for getting or sending files using URL syntax.
- libcurl: a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.
- PHP/cURL: The module for PHP that makes it possible for PHP programs to use libcurl.
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!
2.Complete the goal
To use curl in PHP is very straightforward. The basic idea of using curl in PHP is
- Initialize a curl session
- Set various options for the session
- Execute and fetch/send data from/to server
- Close the session
To complete our goal, the code is simple as
- step1: Initialize a curl session use curl_init().
- step2: Set option for CURLOPT_URL. This value is the URL which we are sending the request to. Append a search term "curl" using parameter "q=". Set option for CURLOPT_RETURNTRANSFER, true will tell curl to return the string instead of print it out. Set option for CURLOPT_HEADER, false will tell curl to ignore the header in the return value.
- step3: Execute the curl session using curl_exec().
- step4: Close the curl session we have created.
- step5: Output the return string.
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:
- curl_error(): For a more advanced system, you should always use curl_error() to check the errors based on the return value(bool) of the curl_exec().
- curl_setopt() : There is a large number of options. Take a look at the manual file. This page has a clear explaination of various options.
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.