A Newbie's guide to using CURL in PHP

Discussion in 'PHP' started by shabbir, Jul 15, 2014.

  1. shabbir

    shabbir Administrator Staff Member

    Jul 12, 2004
    Likes Received:
    Trophy Points:
    CURL is a command line tool for sending and receiving files or other data with URL syntax.CURL supports many protocols for example HTTP, FTP, HTTPS, TELNET, etc. When it comes to PHP/CURL, it is a module that allow us to use libcurl PHP library.


    We can use PHP/CURL for HTTP form based upload, FTP upload, Basic authentication Site data extraction and many others.There are also many other way to do all these tasks with PHP, but this require lots of code and logic and sometimes core PHP functions lack in flexibility and error handling.Also there are some tasks that we can't do simply, like authentication, file upload, etc. which we can do easily with CURL.

    Using CURL

    There are four basic step to handle any request with curl:
    1. Initialize a curl session with the function curl_init()
    $var1 = curl_init();
    2. Set various options for the session with the function curl_setopt()
    curl_setopt($var1, CURLOPT_URL, "http://www.somewebsite.com");
    3. Execute and fetch/send data from the server useing curl_exec()
    $var2 = curl_exec();
    4. Close the CURL session with the function curl_close()
    Now we can write a basic working code for describing a CURL/PHP as,
    //initialize CURL
    $var1 = curl_init();
    $var2 = fopen("some_data.html", "w");
    //set the options,URL.
    curl_setopt($var1, CURLOPT_URL, "http://www.somewebsite.com");
    //Set the option, File
    curl_setopt($var1, CURLOPT_FILE, $var2);
    //Set the option, Header
    curl_setopt($var1, CURLOPT_HEADER, 0);
    //Execute and fetch the resulting HTML output.
    //Closes the CURL session
    Now once we have defined basic CURL requirements so now we can perform various task like error handeling, getting information,uploading files, etc.

    Checking for Errors

    To check errors in the above program we can check errors as:
    $result = curl_exec($var2);
    if($result === FALSE){ //we want to be here more specific.
        //Display Errors with the function curl_error().
        echo "CURL ERROR:" . curl_error($var1);

    Getting information about the link

    We can also get the most complete information about any URL with the CURL function curl_getinfo() and store it into an array. Example:
    $info = curl_getinfo($var1);
    echo $info['url'] . 'get the total time' .$info['total_time']. ' to load';
    Following information is included in the returned array:
    • "url"
    • "content_type"
    • "http_code"
    • "header_size"
    • "request_size"
    • "filetime"
    • "ssl_verify_result"
    • "redirect_count"
    • "total_time"
    • "namelookup_time"
    • "connect_time"
    • "pretransfer_time"
    • "size_upload"
    • "size_download"
    • "speed_download"
    • "speed_upload"
    • "download_content_length"
    • "upload_content_length"
    • "starttransfer_time"
    • "redirect_time"
    Getting internal and external URLs of any URL

    We can get the links of any page with the following code:

    //Set the regeular expression range
    //fetches the data
    $html = $this->curl_fetch_head_data('http://www.go4expert.com');
    echo '<h3>Links On Page: </h3><br>';
    foreach($links as $link){
        echo "Links".": ". $link."<br>";
    The above program output the links to any website from a particular url.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice