What does cURL mean in PHP?

cURL is a PHP library and a command line tool (like wget) that helps you send files and also download data over HTTP and FTP. It supports proxies, you can transfer data over SSL connections, you can set cookies and even get files that are behind a login.

Here’s a basic example on how to use cURL in your PHP project:

    function cURL() {

        // Create a new cURL resource
        $curl = curl_init();

        if (!$curl) {
            die("Couldn't initialize a cURL handle");
        }

        // Set the file URL to fetch through cURL
        curl_setopt($curl, CURLOPT_URL, "http://ctrlq.org/");

        // Set a different user agent string (Googlebot)
        curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');

        // Follow redirects, if any
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

        // Fail the cURL request if response code = 400 (like 404 errors)
        curl_setopt($curl, CURLOPT_FAILONERROR, true);

        // Return the actual result of the curl result instead of success code
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        // Wait for 10 seconds to connect, set 0 to wait indefinitely
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);

        // Execute the cURL request for a maximum of 50 seconds
        curl_setopt($curl, CURLOPT_TIMEOUT, 50);

        // Do not check the SSL certificates
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        // Fetch the URL and save the content in $html variable
        $html = curl_exec($curl);

        // Check if any error has occurred
        if (curl_errno($curl))
        {
            echo 'cURL error: ' . curl_error($curl);
        }
        else
        {
            // cURL executed successfully
            print_r(curl_getinfo($curl));
        }

        // close cURL resource to free up system resources
        curl_close($curl);
    }

PHP

ShareWhatsAppTwitterFacebook

What does cURL mean in PHP?

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

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.

Introduction

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!

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.

More

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. Take a look at the manual file. This page has a clear explaination of various options.

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.

If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.

What is the use of cURL?

Client URL (cURL, pronounced “curl”) is a command line tool that enables data exchange between a device and a server through a terminal. Using this command line interface (CLI), a user specifies a server URL (the location where they want to send a request) and the data they want to send to that server URL.

What does cURL stand for?

cURL, which stands for client URL, is a command-line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.

What is equivalent of cURL in PHP?

cURL is a module in PHP with we can use libcurl. The libcurl is a library that is used in PHP to create connection and communicate with various kinds of different servers which may have different type of protocols.

What is cURL used for in PHP?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.