How to use cURL using php

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to use cURL using php

Post by Neo » Sun Feb 28, 2010 11:13 pm

HP includes an easy to use interface for the cURL library. This means you can easily communicate with other servers using a variety of protocols. It is commonly used to access web service APIs such as Twitter. This tutorial will explain the basics and show some usage examples.

There are 4 main functions you need to know to use cURL: curl_init, curl_setopt, curl_exec, and curl_close. The process generally goes like this:
  1. start a cURL session and get a handle (curl_init)
  2. set the options for the session (curl_setopt)
  3. execute the session (curl_exec)
  4. close the session (curl_close)
The handle returned by curl_init is used as the first parameter in the other functions.

All the available options are listed on the curl_setopt manual page with detailed descriptions.

Simple Example
This example shows how to simply grab the contents of a web page. The data is stored in a variable so you can do whatever you want with it. This can be applied to any file in any format (such as XML). The result is identical to using file_get_contents, but faster.

Code: Select all

<?php
// specify the URL to request
$url = 'http://www.example.com/index.htm';
 
// create cURL session
$ch = curl_init();
 
// set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// return the response instead of printing
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// send the request and store the response in $resp
$resp= curl_exec($ch);
 
// end the session
curl_close($ch);
?>
Filling Out Forms
Let's say a website has a form like this that you want to submit via PHP.

Code: Select all

<form method="post" action="form.php">
   <input type="text" name="name" />
   <input type="text" name="color" />
</form> 
The example below shows how that can be accomplished. All you need to do is set the appropriate method (CURLOPT_POST), and set an array with the form data (CURLOPT_POSTFIELDS).

Code: Select all

<?php
// specify the URL to request
$url = 'http://www.example.com/form.php';
// set up data to send to the form
$data = array('name' => 'Joe', 'color' => 'red');
 
// create cURL session
$ch = curl_init();
 
// set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// return the response instead of printing
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set request method to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the data to send
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
// send the request and store the response in $resp
$resp= curl_exec($ch);
 
// end the session
curl_close($ch);
?>
Authentication
One advantage to using cURL is that is allows you to easily handle authentication. This is useful since some APIs use HTTP authentication. You just need to set the authentication type (CURLOPT_HTTPAUTH) and the credentials (CURLOPT_USERPWD). Credentials are a string in the format "username:password".

Code: Select all

<?php
// specify the URL to request
$url = 'http://www.example.com/private/index.htm';
// specify your credentials
$auth = 'username:password';
 
// create cURL session
$ch = curl_init();
 
// set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// return the response instead of printing
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set the type of authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// set the credentials
curl_setopt($ch, CURLOPT_USERPWD, $auth);
 
// send the request and store the response in $resp
$resp= curl_exec($ch);
 
// end the session
curl_close($ch);
?>
Conclusion
As you can see, cURL is really simple to use once you understand the basic process. You basically tell cURL what you want by setting options, and it does the rest. Any comments or suggestions are welcome, and if you have more examples post away!
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to use cURL using php

Post by Rksk » Mon Apr 04, 2011 2:09 pm

Code: Select all

$agent ="SonyEricssonW200i/R4JA Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1";
$url="http://www.example.com/index.htm";
$cookie_file_path = "cookie.txt"; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fie_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);

print $html; 
We can set USERAGENT name using curl_setopt($ch, CURLOPT_USERAGENT, $agent);.
Also can save & send cookies using,
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fie_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
Post Reply

Return to “PHP & MySQL”