1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A Newbie's guide to using CURL in PHP

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

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.

    Uses



    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()
    Code:
    $var1 = curl_init();
    2. Set various options for the session with the function curl_setopt()
    Code:
    curl_setopt($var1, CURLOPT_URL, "http://www.somewebsite.com");
    3. Execute and fetch/send data from the server useing curl_exec()
    Code:
    $var2 = curl_exec();
    4. Close the CURL session with the function curl_close()
    Code:
    curl_close($var1);
    Now we can write a basic working code for describing a CURL/PHP as,
    Code:
    <?php
    //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.
    curl_exec($var1);
    //Closes the CURL session
    curl_close($var1);
    fclose($var2);
    ?> 
    
    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:
    Code:
    $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:
    Code:
    curl_exec($var);
    $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
    Code:
    $regex='|<a.*?href="(.*?)"|';
    //fetches the data
    $html = $this->curl_fetch_head_data('http://www.go4expert.com');
    preg_match_all($regex,$html,$parts);
    $links=$parts[1];
    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