Webmaster Forum
Go Back   Webmaster forum » ..:: Web Design ::.. » Mod

Reply
 
Thread Tools Display Modes
  #1  
Old 14-06-2009
vinod vinod is offline
Senior Member
 
Join Date: Jun 2009
Posts: 248
iTrader: (0)
vinod is on a distinguished road
Default How to Use CURL With PHP?

How to Use CURL With PHP
Introduction

CURL is a library created by Daniel Stenberg, which allows you to connect and communicate with many different types of servers through various protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+p***word authentication.
Requirements

You will first need to install the lubcurl package if not already installed on your webserver. PHP requires that you use libcurl 7.0.2-beta or higher. In PHP 4.2.3, you will need libcurl version 7.9.0 or higher. From PHP 4.3.0, you will need a libcurl version that’s 7.9.8 or higher. PHP 5.0.0 requires a libcurl version 7.10.5 or greater.
Getting Started

To initiate a CURL session we use the curl_init() function. There are many different options that we are allowed to set with the curl_setopt() function. Once CURL has been setup the way we want, we need to execute the request with the curl_exec() function. Here is an example of a simple script to return the contents of Google’s homepage.

PHP Code:
 // Initialize the CURL library
$cURL curl_init();

// Set the URL to execute
curl_setopt($cURLCURLOPT_URL"http://www.google.com");

// Set options
curl_setopt($cURLCURLOPT_HEADER1);
curl_setopt($cURLCURLOPT_RETURNTRANSFER1);

// Execute, saving results in a variable
$strPage curl_exec($cURL);

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);
?> 
CURL and Form Data

We can also use CURL to post data to a form. The default method of sending form data with CURL is in GET, but in this example we will use POST to submit a search term to an imaginary form.

PHP Code:
 // Setup a string with the form parameters in it
$strParameters "query=dog";

// Initialize the CURL library
$cURL curl_init($cURL);

// Set the URL to execute
curl_setopt($cCURLCURLOPT_URL"http://www.mywebserver.com/mysearch.php");

// Set options
curl_setopt($cCURLCURLOPT_HEADER1);
curl_setopt($cCURLCURLOPT_RETURNTRANSFER1);
curl_setopt($cCURLCURLOPT_POST1);
curl_setopt($cCURLCURLOPT_POSTFIELDS$strParameters);

// Execute, saving results in a variable
$strPage curl_exec();

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);

?> 
CURL and Proxy Support

CURL has support for proxies, including SSL. Below we refer to the code snippet in the first example, but modify it to use a proxy.

PHP Code:
 // Initialize the CURL library
$cURL curl_init();

// Set the URL to execute
curl_setopt($cURLCURLOPT_URL"http://www.google.com");

// Set options
curl_setopt($cURLCURLOPT_HEADER1);
curl_setopt($cURLCURLOPT_RETURNTRANSFER1);
curl_setopt($cCURLCURLOPT_HTTPPROXYTUNNEL1);
curl_setopt($cCURLCURLOPT_PROXY"myproxy.com:1080");
curl_setopt($cCURLCURLOPT_PROXYUSERPWD"mysuername:myp***word");

// Execute, saving results in a variable
$strPage curl_exec($cURL);

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);
?> 
CURL with Authentication

CURL comes with support for most forms of HTTP authentication, including HTTP basic, digest, GSS and NTLM. We will use out code snippet from the second example, but modify it to use basic authentication.

[php] // Setup a string with the form parameters in it
$strParameters = "query=dog";

// Initialize the CURL library
$cURL = curl_init();

// Set the URL to execute
curl_setopt($cCURL, CURLOPT_URL, "http://www.mywebserver.com/mysearch.php");

// Set options
curl_setopt($cCURL, CURLOPT_HEADER, 1);
curl_setopt($cCURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cCURL, CURLOPT_POST, 1);
curl_setopt($cCURL, CURLOPT_POSTFIELDS, $strParameters);
curl_setopt($cCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(CURLOPT_USERPWD, "[myusername]:[myp***word]");

// Execute, saving results in a variable
$strPage = curl_exec($cURL);

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);

?>
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:39 PM.


Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.