Curl in PHP

PHP/cURL: 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”.

Table Of Content

  1. Introduction
  2. Complete the goal
  3. More
  4. The End



    Firstly let us understand the concepts of curl, libcurl and PHP/cURL.

    1. curl: A command line tool for getting or sending files using URL syntax.
    2. 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.
    3. PHP/cURL: The module for PHP that makes it possible for PHP programs to use libcurl.

    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!

    2.Complete the goal

    To use curl in PHP is very straightforward. The basic idea of using curl in PHP is

    1. Initialize a curl session
    2. Set various options for the session
    3. Execute and fetch/send data from/to server
    4. 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:

    1. 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().
    2. curl_setopt() : There is a large number of options.
    4.The End

    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.