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

cURL in PHP Examples

Discussion in 'PHP' started by pradeep, Dec 12, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    PHP 4.0.2 onwards libcurl support was added in PHP, libcurl is a free client library initially written by Daniel Stenberg which allows us to communicate with servers using a variety of protocols like HTTP,HTTPS,FTP,SMTP,LDAP, etc. In this article we'll be looking at enabling libcurl support in PHP and using it for accessing URLs with HTTP/HTTPs, I'll try to discuss interesting uses of other protocols in some other article soon.

    Installing/Enabling cURL



    In order to use PHP cURL you need to have libcurl installed on your system. If you are compiling your PHP installation, compile PHP --with-curl[=DIR] where DIR is the directory containing the library source code.

    If your using a Debian/Ubuntu system, this should be enough:

    Code:
    $ apt-get install php5-curl
    In case you are using Windows/XAMPP, just uncomment this line in the php.ini file.

    Code:
    ;extension=php_curl.dll
    After installing, restart webserver.

    Basic Usage



    You can set various parameters for cURL, like headers, content type, etc. You can specify post fields, cookies, all that can be configured can be done with cURL.

    In the example below, we'll just call a URL.
    PHP:
    <?
    $curl_handler curl_init();

    // set options
    curl_setopt($curl_handle,CURLOPT_URL,'http://www.go4expert.com?run=cron');

    // execute
    curl_exec($curl_handle);

    // finally close & free resources
    curl_close($curl_handle);
    ?>
    Now, it might happen we want the output of the URL we just invoked, or in other words fetch the contents of the URL.
    PHP:
    <?
    $curl_handler curl_init();

    // set options
    curl_setopt($curl_handle,CURLOPT_URL,'http://www.go4expert.com?format=json');
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);

    // execute
    $content curl_exec($curl_handle);

    // finally close & free resources
    curl_close($curl_handle);

    [
    URL="http://www.php.net/print"]print[/URL$content;
    ?>

    Advanced Usage



    Now, let us look at some advanced configuration options like setting cookies, specifying cookie jars, POSTing, following location, etc.

    PHP:
    <?
    $curl_handle curl_init();

    // set options
    curl_setopt($curl_handleCURLOPT_URL,'http://www.go4expert.com?format=json&report_id=1');
    curl_setopt($curl_handleCURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl_handleCURLOPT_COOKIEJAR'/tmp/cookie_reports.txt');
    curl_setopt($curl_handleCURLOPT_COOKIEFILE'/tmp/cookie_reports.txt');

    // you may manually specify cookies
    curl_setopt($curl_handleCURLOPT_COOKIE'user=Anjali;');

    // you can ask libcurl to follow 3xx redirects
    curl_setopt($curl_handleCURLOPT_FOLLOWLOCATIONtrue);

    // set post fields
    $post_data = [URL="http://www.php.net/array"]array[/URL](
    'date_start' => '2012-11-11',
    'date_end' => '2012-12-12',
    'columns' => 'ip,count,date'
    );

    curl_setopt($curl_handleCURLOPT_POSTtrue);
    curl_setopt($curl_handleCURLOPT_POSTFIELDS$data);
    ?>
    Now, that's easy, isn't it? For more options that are available refer to http://php.net/manual/en/function.curl-setopt.php
     
  2. jeeva1388

    jeeva1388 New Member

    Joined:
    Jan 19, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 ...
     

Share This Page