This version of the page http://ua2.php.net/curl (0.0.0.0) stored by archive.org.ua. It represents a snapshot of the page as of 2010-06-18. The original page over time could change.
PHP: cURL - Manual
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

  • PHP Manual
  • Function Reference
  • Other Services
  • AMQP
  • cURL
  • FAM
  • FTP
  • Gupnp
  • Hyperwave
  • Hyperwave API
  • HTTP
  • Java
  • LDAP
  • Lotus Notes
  • Memcache
  • Memcached
  • Gearman
  • mqseries
  • Gopher
  • Network
  • SAM
  • SNMP
  • Sockets
  • SSH2
  • Stomp
  • SVN
  • TCP
  • YAZ
  • YP/NIS
Introduction AMQPQueue::unbind
Last updated: Fri, 11 Jun 2010

view this page in

Client URL Library

  • Introduction
  • Installing/Configuring
    • Requirements
    • Installation
    • Runtime Configuration
    • Resource Types
  • Predefined Constants
  • Examples
    • Using PHP's cURL module to fetch the example.com homepage
  • cURL Functions
    • curl_close — Close a cURL session
    • curl_copy_handle — Copy a cURL handle along with all of its preferences
    • curl_errno — Return the last error number
    • curl_error — Return a string containing the last error for the current session
    • curl_exec — Perform a cURL session
    • curl_getinfo — Get information regarding a specific transfer
    • curl_init — Initialize a cURL session
    • curl_multi_add_handle — Add a normal cURL handle to a cURL multi handle
    • curl_multi_close — Close a set of cURL handles
    • curl_multi_exec — Run the sub-connections of the current cURL handle
    • curl_multi_getcontent — Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
    • curl_multi_info_read — Get information about the current transfers
    • curl_multi_init — Returns a new cURL multi handle
    • curl_multi_remove_handle — Remove a multi handle from a set of cURL handles
    • curl_multi_select — Wait for activity on any curl_multi connection
    • curl_setopt_array — Set multiple options for a cURL transfer
    • curl_setopt — Set an option for a cURL transfer
    • curl_version — Gets cURL version information


Introduction AMQPQueue::unbind
Last updated: Fri, 11 Jun 2010
 
add a note User Contributed Notes
cURL
cliffclof atty gmail dotty com
05-Apr-2010 09:21
A solution that addresses repeat calls to the same set of urls using the same connection simulating frequent ajax calls in separate browser tabs.

In a unique situation you may need to set a cookie then use that cookie for multiple separate persistent connections using the same session cookie.  The problem is the session cookie may change while your doing your persistent calls.  If you set every curl handle to update a shared cookiejar on close you may overwrite the new found session value with the old session value depending on the closing order of your handles. Also, because the cookiejar is only written to on a curl_close, you may be using dissimilar or old session info in some of your 'faked browser tabs'.

To solve this problem I created a unique handle that opens and closes specifically to set a cookie file using CURLOPT_COOKIEJAR. Then I just use the read-only CURLOPT_COOKIEFILE on the multiple separate persistent handles.

This solves the problem of shared cookies fighting to write their values and keep persistent calls using the most up to date cookie information.

Note: In my case the multiple calls were in a while loop and I was using php in shell.  The session cookie value plus browser type limited the number of connections available and i wanted to use the max connections per session.
kalyan at kalyanchakravarthy dot net
01-Mar-2010 03:32
When using curl with cookies, Relative path doesn't seem to work.

Use absolute path for setting the variables CURLOPT_COOKIEFILE & CURLOPT_COOKIEJAR.

To make life easier use the realpath("file.txt") function to get the absolute path.
Chris Pollett
19-Jan-2010 02:56
This is something obscure I noticed... If you try to use something like:

<?php
curl_setopt
($agent, CURLOPT_POST, true);
curl_setopt($agent, CURLOPT_POSTFIELDS, $post_data);   
?>

so that you POST something to a URL. Then if you screwed up your URL slightly like:

http://www.example.com

rather than

http://www.example.com/ (note the slash)

hence, causing a redirect, then your posted data won't be sent once the redirect occurs.
eugene at ultimatecms dot co dot za
07-Dec-2009 11:38
A simple whois/domain availability check using cURL:

<?php

function domain_check($domain) {

$data = 'http://'.$domain;

// Create a curl handle to a non-existing location
$ch = curl_init($data);

// Execute
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);

// Check if any error occured
if(curl_errno($ch))
{
    return
'<span style="color:#22c922">The domain is available!</span>';
} else {
    return
'<span style="color:#c92222">The domain is not available</span>';
}

// Close handle
curl_close($ch);
}

// Usage:
if(isset($_POST['domain'])) {
 echo
domain_check($_POST['domain'].$_POST['tld']);
}

?>

<form method="POST" action="">
http:// <input type="text" name="domain">
<select name="tld">
<option value=".com">.com</option>
<option value=".net">.net</option>
<option value=".biz">.biz</option>
</option>
<input type="submit" value="Check">
</form>
artem at zabsoft dot co dot in
11-May-2009 06:43
Hey I modified script for php 5. Also I add support server auth. and fixed some little bugs on the script.

[EDIT BY danbrown AT php DOT net: Original was written by (unlcuky13 AT gmail DOT com) on 19-APR-09.  The following note was included:
Below is the my way of using through PHP 5 objecte oriented encapsulation to make thing easier.]

<?php
 
class mycurl {
     protected
$_useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1';
     protected
$_url;
     protected
$_followlocation;
     protected
$_timeout;
     protected
$_maxRedirects;
     protected
$_cookieFileLocation = './cookie.txt';
     protected
$_post;
     protected
$_postFields;
     protected
$_referer ="http://www.google.com";

     protected
$_session;
     protected
$_webpage;
     protected
$_includeHeader;
     protected
$_noBody;
     protected
$_status;
     protected
$_binaryTransfer;
     public   
$authentication = 0;
     public   
$auth_name      = '';
     public   
$auth_pass      = '';

     public function
useAuth($use){
      
$this->authentication = 0;
       if(
$use == true) $this->authentication = 1;
     }

     public function
setName($name){
      
$this->auth_name = $name;
     }
     public function
setPass($pass){
      
$this->auth_pass = $pass;
     }

     public function
__construct($url,$followlocation = true,$timeOut = 30,$maxRedirecs = 4,$binaryTransfer = false,$includeHeader = false,$noBody = false)
     {
        
$this->_url = $url;
        
$this->_followlocation = $followlocation;
        
$this->_timeout = $timeOut;
        
$this->_maxRedirects = $maxRedirecs;
        
$this->_noBody = $noBody;
        
$this->_includeHeader = $includeHeader;
        
$this->_binaryTransfer = $binaryTransfer;

        
$this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt';

     }

     public function
setReferer($referer){
      
$this->_referer = $referer;
     }

     public function
setCookiFileLocation($path)
     {
        
$this->_cookieFileLocation = $path;
     }

     public function
setPost ($postFields)
     {
       
$this->_post = true;
       
$this->_postFields = $postFields;
     }

     public function
setUserAgent($userAgent)
     {
        
$this->_useragent = $userAgent;
     }

     public function
createCurl($url = 'nul')
     {
        if(
$url != 'nul'){
         
$this->_url = $url;
        }

        
$s = curl_init();

        
curl_setopt($s,CURLOPT_URL,$this->_url);
        
curl_setopt($s,CURLOPT_HTTPHEADER,array('Expect:'));
        
curl_setopt($s,CURLOPT_TIMEOUT,$this->_timeout);
        
curl_setopt($s,CURLOPT_MAXREDIRS,$this->_maxRedirects);
        
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
        
curl_setopt($s,CURLOPT_FOLLOWLOCATION,$this->_followlocation);
        
curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation);
        
curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation);

         if(
$this->authentication == 1){
          
curl_setopt($s, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
         }
         if(
$this->_post)
         {
            
curl_setopt($s,CURLOPT_POST,true);
            
curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);

         }

         if(
$this->_includeHeader)
         {
              
curl_setopt($s,CURLOPT_HEADER,true);
         }

         if(
$this->_noBody)
         {
            
curl_setopt($s,CURLOPT_NOBODY,true);
         }
        
/*
         if($this->_binary)
         {
             curl_setopt($s,CURLOPT_BINARYTRANSFER,true);
         }
         */
        
curl_setopt($s,CURLOPT_USERAGENT,$this->_useragent);
        
curl_setopt($s,CURLOPT_REFERER,$this->_referer);

        
$this->_webpage = curl_exec($s);
                  
$this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE);
        
curl_close($s);

     }

   public function
getHttpStatus()
   {
       return
$this->_status;
   }

   public function
__tostring(){
      return
$this->_webpage;
   }
}
?>

[EDIT BY danbrown AT php DOT net: Contains a bugfix supplied by "roetsch.beni at googlemail (dot) com" on 02-AUG-09, with the following note: "Fixes the bugfix: 417 bug at lighthttp server."]
admin at jeremyzaretski dot com
18-Mar-2009 04:14
I wanted to create a script that acted as a bridge between an external server and an internal server, wherein the internal server was not connected to the internet, but had information required by the users connecting to the external server. I hatched the idea to use curl to connect from the external server to the internal server (using request variables to send queries) and return everything (data and headers) returned by the file server.

After being driven mad by segmentation faults and crashes because the curl_exec didn't like me having the CURLOPT_HEADERFUNCTION's function directly dumping the header:

<?php
function Duplicate_Header($curl, $header)
{
   
header($header);
    return
strlen($header);
}
?>

... I tried (on a whim) duplicating and trimming the header and passing the duplicate to the header function...

<?php
function Duplicate_Header($curl, $header)
{
   
$duplicate = trim($header);
   
header($duplicate);
    return
strlen($header);
}
?>

Which worked just fine. I don't know if this is just some quirk of PHP4 or my lack of understanding of how the curl and header function works.
web at davss dot com
26-Jan-2009 10:00
Here is a reusable curl object - please help me improve it. There is no erro handling (I don't need it) which can be implemented

<?php
class cURL {

   
# defaul global options
   
var $opts = array(
           
CURLOPT_HEADER => FALSE,
           
CURLOPT_RETURNTRANSFER => TRUE
   
);

    function
cURL(){
    }

    function
r($ch,$opt){
       
# assign global options array
       
$opts = $this->opts;
       
# assign user's options
       
foreach($opt as $k=>$v){$opts[$k] = $v;}
       
curl_setopt_array($ch,$opts);
       
curl_exec($ch);
       
$r['code'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
       
$r['cr'] = curl_exec($ch);
       
$r['ce'] = curl_errno($ch);
       
curl_close($ch);
        return
$r;
    }

    function
get($url='',$opt=array()){
       
# create cURL resource
       
$ch = curl_init($url);
        return
$this->r($ch,$opt);
    }

    function
post($url='',$data=array(),$opt=array()){
       
# set POST options
       
$opts[CURLOPT_POST] = TRUE;
       
$opts[CURLOPT_POSTFIELDS] = $data;

       
# create cURL resource
       
$ch = curl_init($url);
        return
$this->r($ch,$opt);
    }
};
$cURL = new cURL();

#
# END of CLASS
#

/*
* test.php files for testing purposes - must be in the same folder undelss you specify $url path
*
<?php
echo " WORKING!!<p>";
if($_GET > 0){
print_r($_GET);
}
elseif($_POST > 0){
print_r($_POST);
}
?>
*/
/*
# EXAMPLE OF USE
$path = "http://"; # path to test.php file
$file = "test.php"; # safe the above code in text.php file
$url = $path . $file;
$post_data = array("FLD1"=>"VAL1","FLD2"=>"VAL2");
# user's curl options that override global if necessary
$o = array(CURLOPT_HEADER => TRUE,CURLOPT_RETURNTRANSFER => TRUE,CURLOPT_FAILONERROR=>TRUE);

function ccc($arr){
    echo "CODE: " . $arr['code'] . "<p>";
    echo "CE: " . $arr['ce'] . "<p>";
    echo "CR: <br />" . $arr['cr'] . "<p>";
}

#POST TEST
$cPost = $cURL->post($url,$post_data,$o); // you can remove options
echo "<h2>cPost</h2>";
ccc($cPost);

#GET TEST
$cGet = $cURL->post($url,$post_data,$o); // you can remove options
echo "<h2>cGet</h2>";
ccc($cGet);
*/

?>
pyromus at gmail dot com
16-Oct-2008 11:37
You can use this class for fast entry

<?php
class cURL {
var
$headers;
var
$user_agent;
var
$compression;
var
$cookie_file;
var
$proxy;
function
cURL($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') {
$this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$this->headers[] = 'Connection: Keep-Alive';
$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';
$this->compression=$compression;
$this->proxy=$proxy;
$this->cookies=$cookies;
if (
$this->cookies == TRUE) $this->cookie($cookie);
}
function
cookie($cookie_file) {
if (
file_exists($cookie_file)) {
$this->cookie_file=$cookie_file;
} else {
fopen($cookie_file,'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions');
$this->cookie_file=$cookie_file;
fclose($this->cookie_file);
}
}
function
get($url) {
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process,CURLOPT_ENCODING , $this->compression);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
if (
$this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return
$return;
}
function
post($url,$data) {
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process, CURLOPT_ENCODING , $this->compression);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
if (
$this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_POST, 1);
$return = curl_exec($process);
curl_close($process);
return
$return;
}
function
error($error) {
echo
"<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
die;
}
}
$cc = new cURL();
$cc->get('http://www.example.com');
$cc->post('http://www.example.com','foo=bar');
?>

[EDIT BY danbrown AT php DOT net: Includes a bugfix provided by "Anonymous" on 01-Dec-2008 @ 06:52.  Also replaced real URL with example.com as per RFC 2606.]

[EDIT BY danbrown AT php DOT net: Includes a bugfix provided by (manuel AT rankone DOT ch) on 24-NOV-09 to properly reference cURL initialization.]
eflash at gmx dot net
05-Oct-2008 04:45
In order to use curl with secure sites you will need a ca-bundle.crt file; here's a PHP script I've written which creates a fresh ca-bundle:
http://www.gknw.net/php/phpscripts/mk-ca-bundle.php
I've also written scripts in other languages, f.e. the Perl one which ships now with curl distributions:
http://curl.haxx.se/lxr/source/lib/mk-ca-bundle.pl
and also a Win32 WSH script if you prefer that:
http://www.gknw.net/vb/scripts/mk-ca-bundle.vbs

HTH, Guenter.
add a note

Introduction AMQPQueue::unbind
Last updated: Fri, 11 Jun 2010
 
 
show source | credits | sitemap | contact | advertising | mirror sites