Home » » CURL Example

CURL Example

Written By 1 on Thursday, January 16, 2014 | 8:10 AM

CURL is a library and command-line tool for transferring data using various protocols like HTTP, FTP, SCP, PUT, POST, POP3 & SMTP etc.
It have two products libcurl and cURL.

cURL: Its command line tool for transferring information.
Libcurl: It is a free client-side URL transfer library.


CURL COMMAND LINE EXAMPLE

Curl Example with "GET" Method (By Default GET)
curl http://php-tutorial-php.blogspot.in/p/sitemap.html

Curl Example with "POST" Method
curl --request POST http://php-tutorial-php.blogspot.in/p/sitemap.html

Curl Example with "PUT" Method
curl --request PUT http://php-tutorial-php.blogspot.in/p/sitemap.html

Curl Example with "DELETE" Method
curl --request DELETE http://php-tutorial-php.blogspot.in/p/sitemap.html

Curl Example with "POST" Method and pass data (Data: username, password & logintype)
curl --request POST http://php-tutorial-php.blogspot.in/p/sitemap.html
--data 'username=user&password=****&logintype=normal' 

Curl Example with "POST" Method and pass data(data in text file)
curl --request POST http://php-tutorial-php.blogspot.in/p/sitemap.html --data @datafile.txt

Curl Example with "POST" Method, pass data and pass header
curl --request POST http://php-tutorial-php.blogspot.in/p/sitemap.html --data @datafile.txt --header 'sessionid:9874563211235884' 

Curl Example with "POST" Method, pass data and pass header (Get Header in response) --include
curl --request POST http://php-tutorial-php.blogspot.in/p/sitemap.html --data @datafile.txt --header 'sessionid:9874563211235884' --include

CURL LIBCURL EXAMPLE

Mostly to get data from an REST API, we use following method in php
echo file_get_contents('http://php-tutorial-php.blogspot.in/p/sitemap.html');
But if, "allow_url_fopen" is disable in php.ini file due to security reason then above will not work. You need to use below code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.co.in");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
echo $output;die;

CURL Example with Post Method with parameter
$url = "http://localhost/test/index2";
$post_data = array(
            "foo" => "bar",
            "foo1" => "testdata"
);
try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $output = curl_exec($ch);
    curl_close($ch);
} catch (Exception $e) {
    echo $e->getMessage();die;
}

File Upload with CURL in PHP
 $file_name_with_full_path = realpath('uploadFile.zip');        
        $url = "http://localhost/test/index2";
        $post_data = array(
            "foo" => "bar",
            "upload" => "@".$file_name_with_full_path
        );
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            $output = curl_exec($ch);
            curl_close($ch);
        } catch (Exception $e) {
            echo $e->getMessage();
            die;
        }
        
Transfer the File through FTP
curl_setopt_array($ch, array(
    CURLOPT_URL => 'ftp://ftp.example.com/test.txt',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERPWD => 'username:password'
));
 
$output = curl_exec($ch);

Set Header and Json data in Curl
$url='http://php-tutorial-php.blogspot.in/';
$jsonData='{"name":"arun","email":"arun@gmail.com"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'X-IHG-MWS-API-Token: ' . X_API_TOKEN,
  'Content-Type: application/json',
  "HTTP_X_FORWARDED_FOR: xxx.xxx.x.xx"
));

curl_setopt($ch, CURLOPT_INTERFACE, "xxx.xxx.x.xx");
echo curl_exec($ch);
curl_close($ch);


Following are some Options which can be set be set in "curl_setopt" function


CURLOPT_REFERER - Set the Http Refer

CURLOPT_USERAGENT - Set the UserAgent

CURLOPT_HEADER - Include the header in response or not

CURLOPT_RETURNTRANSFER - Print the output or not

CURLOPT_URL - Set the URL (Uniform Resource Locator)

CURLOPT_POST - set 0 or 1 for post method

CURLOPT_POSTFIELDS - set the data which you want to post

CURLOPT_FOLLOWLOCATION - if set true, cURL will follow redirects

CURLOPT_CONNECTTIMEOUT -Total seconds to spend attempting to connect

CURLOPT_TIMEOUT - Total Seconds to allow cURL to execute

    0 Comment:

    Post a Comment