PHP 7.1.12 Released

CURL

add a note add a note

User Contributed Notes 19 notes

up
28
artem at zabsoft dot co dot in
8 years ago
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."]
up
25
frank at interactinet dot com
6 years ago
I wrote the following to see if a submitted URL has a valid http response code and also if it responds quickly.

Use the code like this:

<?php
$is_ok
= http_response($url); // returns true only if http response code < 400
?>

The second argument is optional, and it allows you to check for  a specific response code

<?php
http_response
($url,'400'); // returns true if http status is 400
?>

The third allows you to specify how long you are willing to wait for a response.

<?php
http_response
($url,'200',3); // returns true if the response takes less than 3 seconds and the response code is 200
?>

<?php
function http_response($url, $status = null, $wait = 3)
{
       
$time = microtime(true);
       
$expire = $time + $wait;

       
// we fork the process so we don't have to wait for a timeout
       
$pid = pcntl_fork();
        if (
$pid == -1) {
            die(
'could not fork');
        } else if (
$pid) {
           
// we are the parent
           
$ch = curl_init();
           
curl_setopt($ch, CURLOPT_URL, $url);
           
curl_setopt($ch, CURLOPT_HEADER, TRUE);
           
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
           
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
           
$head = curl_exec($ch);
           
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
           
curl_close($ch);
           
            if(!
$head)
            {
                return
FALSE;
            }
           
            if(
$status === null)
            {
                if(
$httpCode < 400)
                {
                    return
TRUE;
                }
                else
                {
                    return
FALSE;
                }
            }
            elseif(
$status == $httpCode)
            {
                return
TRUE;
            }
           
            return
FALSE;
           
pcntl_wait($status); //Protect against Zombie children
       
} else {
           
// we are the child
           
while(microtime(true) < $expire)
            {
           
sleep(0.5);
            }
            return
FALSE;
        }
    }
?>

Hope this example helps.  It is not 100% tested, so any feedback [sent directly to me by email] is appreciated.
up
9
qrworld.net
3 years ago
Here you have a function that I use to get the content of a URL using cURL:

function getUrlContent($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode>=200 && $httpcode<300) ? $data : false;
}

The source comes from this website:

http://softontherocks.blogspot.com/2014/11/descargar-el-contenido-de-una-url.html
up
6
gmail@asmqb7
1 year ago
WARNING WARNING

In this example: http://php.net/manual/en/book.curl.php#102885 by "frank at interactinet dot com"

There's a small bug in

<?php

        
...

         elseif(
$status == $httpCode)
            {
                return
TRUE;
            }

            return
FALSE;
           
pcntl_wait($status); //Protect against Zombie children
       
} else {
           
// we are the child
           
while(microtime(true) < $expire)

         ...

?>

The code will immediately leave the function at the `return`, and pcntl_wait() will NEVER be executed, under any circumstances.

I can't see any other issues with this function however.
up
5
artax_N_O_S_P_A_M_erxes2 at iname dot com
7 years ago
I needed to use cURL in a php script to download data using not only SSL for the server authentication but also for client authentication.
On a default install of Fedora, setting up the proper cURL parameters, I would get an error:

$ php curl.php
Peer certificate cannot be authenticated with known CA certificates

The data on http://curl.haxx.se/docs/sslcerts.html was most useful. Indeed, toward to bottom it tells you to add a missing link inside /etc/pki/nssdb to use the ca-bundle.crt file. You do it so:

# cd /etc/pki/nssdb
# ln -s /usr/lib64/libnssckbi.so libnssckbi.so

Now you can do client authentication, provided you have your certificate handy with:

<?php
$data
= "<soap:Envelope>[...]</soap:Envelope>";
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://example.com/path/for/soap/url/");
curl_setopt($tuCurl, CURLOPT_PORT , 443);
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_SSLVERSION, 3);
curl_setopt($tuCurl, CURLOPT_SSLCERT, getcwd() . "/client.pem");
curl_setopt($tuCurl, CURLOPT_SSLKEY, getcwd() . "/keyout.pem");
curl_setopt($tuCurl, CURLOPT_CAINFO, getcwd() . "/ca.pem");
curl_setopt($tuCurl, CURLOPT_POST, 1);
curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));

$tuData = curl_exec($tuCurl);
if(!
curl_errno($tuCurl)){
 
$info = curl_getinfo($tuCurl);
  echo
'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
} else {
  echo
'Curl error: ' . curl_error($tuCurl);
}

curl_close($tuCurl);
echo
$tuData;
?>
up
2
ramez at dot dontspan dot zegenie dot com
7 years ago
CURL failed with PHP5.3 and Apache2.2.X on my Windows 7 machine.

It turns out that it's not enough to copy the two dll's mentioned (libeay32 and sslea32) from the php folder into your system32 folder. You HAVE TO UNBLOCK THESE TWO FILES.

Right click the file, select unblock, for each one. Then restart Apache.

Another very handy security feature added into Windows.
up
3
pyromus at gmail dot com
9 years ago
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_Hgspan class="default">fclosefclose
fclosebsp;        elseif(fclosefclosefclosefclosefclosefclosefclosefclosefclosefczss="k RLOP"default">CURLOPT_TIMEOUT, 30);
if (
$this->proxy);
if (
$this($process, CURLOPT_PROXY$data)));

$data
., $data., CURLOPT_TIMEOUTup
fczss="k RLOP"cdefault">30, )) {
$thiscoh this function www.> Peer cert
) $this30, , $thifault">coh this function www.> Peer cert
) cookies) $this; <[EDIT BY danbr.&class> Peer cert as>

    &ne2119p; n class="string">'Took ' ($tuCurl);
  echo 'Took '
($info['total_time'$inbsp; . ' seconds to send a req2119p;aan>ramez at dodserchao769390531pan 163['url2119p; &];
} else 7-06-07 07:237 09:19"><5 m 'Curl error: '
$this, support ess
"ETURwait()* @ will /span>"referRwait()* @ will int $"defeywon cs"defeywopan>(It turns out that it's not enougt">$cookie_file
fault">$data)));

curl_setopt
(CURLOPT_TIMEOUT, .curl_setoptCURLOPT_TIMEOUT, (
7 years aord"an fault">$data)));

curl_setopt
$thifault">coh this functibr /error:

$ php curl.php
)
, , ,
((($process, <)atent-length: ((<[tent-length: ((curl_setopt(>(fclose((fclose(">>(fclose((,'gzip'(('gzip'(((>(, (($data($data">ss="keyword">, n class="dreferRault">$dataif ($data(($data"/clientHddefault"psyn class="="useme dot com
($data"/client.pem"n class="="useme dot com$process$data)));

<">(curl_setopt
$data((span class="default">curl_setopt($tuCurl, (curl_setop)atent-length: if (1);
(curl_setop)>((
(span class="default">curl_setopt($data
)));

<">(curl_setop)atent-length: $processproxy);
if (
<">(curl_setop)atent-length: ($processt">curl_errno$this, support ess
"ETURwait()* @ will , "referRwait()* @ will int $"defeywon cs"defeywopan>"fault"Rwait()* @ord">(It turns out that it's not enougt">$cookie_file
fault">$data)));

curl_setopt
(CURLOPT_ENCODING (
CURLOPT_TIMEOUT, .curl_setoptCURLOPT_TIMEOUT, (<[]k />It {tent-length: ((($process, <)atent-length: (
7 years aord"an fault">$data)));

curl_setopt
$thifault">coh this functibr /error:

$ php curl.php
curl_setop) =keyword">,
, ,
((<[tent-length: ((curl_setopt(>(fclose((fclose((CURLOPT_ENCODING ((fclose(">>(fclose((,'gzip'(('gzip'(((>(, (((">>((curl_setop)>($data, n class="dfault"an class="default">curl_setopatent-length: if ($data($data">ss="keyword">, n class="dreferRault">$="default">curl_setopatent-length: if ($data(($data"/clientHddefault"psyn class="="useme dot com($data"/client.pem"n class="="useme dot com$process$data)));

<">(curl_setopt
$data$processt">c class="default">curl_setopt($tuCurl, (curl_setop)atent-length: if (1);
(curl_setop)>((
(span class="default">curl_setoptif ($data)));

<">(curl_setop)atent-length: $processproxy);
if (
<">(curl_setop)atent-length: ($processt">curl_errno$this$pworRrn>($processworRring">fault">$data)));
.?n>
$ php curl.php
?n>curl_setop)atent-l
if(!
($this$pan cRrn>($processan cRring">fault">$data)));
.?n>
$ php curl.php
?n>curl_setop,[])atent-l
if(!
)){
 
$info = <2165t">n class="string">'Took ' ($tuCurl);
  echo n class="string">'Took '
($info
['total_time'$isp; .
' seconds to send a req2165t"aan>ramez at dotuspendmezthpan dot zegenie dot com$];
} else 7-09-19 07:137 09:19"><2 m 'Curl error: '
n00646">
.span can c onlyn>);s "WoW"pan n can c ult"helpclas> < this functibr /lupremetencert/bg">-w
$ php curl.php
-w)){
 
ramez at doef
7 years ago
?n sin"clllineed >< this functid="www.gknw.net/ cl/ clscripts/mk-/a-bundr c cl
TRUPeti">< this functid="" ti.haxx.se/lxr/sn>
and a&nbsapan 32 WSH script if"preferard">:
< this functid="www.gknw.net/vb/scripts/mk-/a-bundr cvbs
)){
  $info = <03600">n class="string">'Took ' ($tuCurl);
  echo n class="string">'Took '
($info['total_time'' seconds to send a req03600"oan>ramez at domadhomez-netlaskpan hotot zegenie dot com$];
} else 1-04-21 01:517 09:19"><6trong>7 years ago
n00646">
TRU
an>rbr />(1) P /tocol ftpesass="supported PHdioabled n slibc ult">curln class="keyword">)){
 
)s decvapaables builtois e" i_build_query());s an clng.pache2.2.X on /span>$thisif(!($processlt">CURLOPT_COOKIEJAR, curl_setopt)));
.,
)));
proxy
);
if (
$dataif(!($processlt">CURLOPT_COOKIEJAR, curl_setopt);
if (
$dataif(!; )aalng_ ti"contan s ti" . Peer cert/alng>
x-vois add">. opcookal willetn c $thisif(!($processlt">CURLOPT_COOKIEJAR, curl_setopt)));
.,
)));
proxy
);
if (
$datacurl_setop,="keyword">)));
.) cookies$dataif(!($processlt">CURLOPT_COOKIEJAR, curl_setopt);
if (
$dataif(!)){
 
n00646">
' seconds to send a re"966p;oan>ramez at doadminpan jeremyzapetskiegenie dot com
<8trong>7 years ago