略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: cURL

2024-12-21

Client URL 库

add a noteadd a note

User Contributed Notes 27 notes

up
43
frank at interactinet dot com
11 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
artax_N_O_S_P_A_M_erxes2 at iname dot com
11 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
6
gmail@asmqb7
6 years 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
1
jcmargentina at gmail dot com
2 years ago
Please note that new versions of curl is using http2 as default, so if you are having some strange errors, 0 http status codes, etc, please explicitly specify the http version in your code.
up
2
qrworld.net
7 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
artem at zabsoft dot co dot in
13 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
2
ramez at dot dontspan dot zegenie dot com
11 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
0
newuser at gmail dot com
3 years ago
FYI cURL support (default enabled, ok) is prerequisite for Installation and Configuration of the Collection Extension of wiki portal. https://pickbestscope.com/
up
-1
eflash at gmx dot net
13 years ago
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.
up
-2
pyromus at gmail dot com
13 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_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.]
up
-1
zle.lc
1 year ago
Sharing is caring, handles included.

<?php

$url_one
= "php.net";
$url_two = "";

$user_agent = 'Mozilla HotFox 1.0';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_one.$url_two);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$res = curl_exec($ch);
curl_close($ch);

$url_two = "lazyphp.net";
$url_one = "";
$res_two = curl_exec($ch);
curl_close($ch);

?>
up
-3
user at gmail dot com
3 years ago
Ratings are maintained by FFBE subreddit's Discord. Newbie questions and question whether a certain unit is good or not should be asked in .. http://ffbeunitranking.com/
up
-6
fred dot knieper at gmail dot com
8 years ago
After a lot of frustration with the fact that nobody has documented which curl commandline options go with which library functions, I discovered that the curl commandline will tell you (in the form of a C program) if you add `--libcurl foo.c`

If you've been struggling with trying to figure out how to get your fancy curl commandline to work in PHP, this makes it a breeze!
up
-6
cliffclof atty gmail dotty com
12 years ago
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.
up
-6
admin at jeremyzaretski dot com
13 years ago
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.
up
-7
shidec00 at yahoo dot com
11 years ago
This anomali only happen on windows.
Server indicates that some variables built by http_build_query() is missing.

<?php
//...
//...
//...
$ping_url = $this->sx_url.'ping.php?'.http_build_query($options);
$message = $this->_post_curl($ping_url);
?>

After debugging i found that $ping_url contain url like :

http://example.com/ping.php?app=1&amp;key=mail&amp;ttd=df52861e

But myserver give "No ttd GET variable" response

This problem fixed by adding optional parameter to make sure that http_build_query() use only '&' as arg separator rather than '&amp;'

<?php
//...
//...
//...
$ping_url = $this->sx_url.'ping.php?'.http_build_query($options,'','&');
$message = $this->_post_curl($ping_url);
?>
up
-9
romet
7 years ago
/*
Example values
url - 'http://example.com'
fields - array('var' => 'value'), or can be empty
auth - 'user:password', or can be empty
by romet, 4.20.2015
*/
function curl($url, $fields = array(), $auth = false){
   
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_VERBOSE, 1);
    curl_setopt($curl, CURLOPT_HEADER, 1);
   
    if($auth){
        curl_setopt($curl, CURLOPT_USERPWD, "$auth");
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    }

    if($fields){       
        $fields_string = http_build_query($fields);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
    }
   
    $response = curl_exec($curl);
    $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    $header_string = substr($response, 0, $header_size);
    $body = substr($response, $header_size);
   
    $header_rows = explode(PHP_EOL, $header_string);
    $header_rows = array_filter($header_rows, trim);
    foreach((array)$header_rows as $hr){
        $colonpos = strpos($hr, ':');
        $key = $colonpos !== false ? substr($hr, 0, $colonpos) : (int)$i++;
        $headers[$key] = $colonpos !== false ? trim(substr($hr, $colonpos+1)) : $hr;
    }
    foreach((array)$headers as $key => $val){
        $vals = explode(';', $val);
        if(count($vals) >= 2){
            unset($headers[$key]);
            foreach($vals as $vk => $vv){
                $equalpos = strpos($vv, '=');
                $vkey = $equalpos !== false ? trim(substr($vv, 0, $equalpos)) : (int)$j++;
                $headers[$key][$vkey] = $equalpos !== false ? trim(substr($vv, $equalpos+1)) : $vv;
            }
        }
    }
    //print_rr($headers);
    curl_close($curl);
    return array($body, $headers);
}

list($d['body'], $d['headers']) = curl2('http://google.com', array(q => '123'));
//POST to google.com with POST var "q" as "123"
   
echo '<pre>';
print_r($d);

---------

OUTPUT:

Array
(
    [headers] => Array
        (
            [0] => HTTP/1.1 405 Method Not Allowed
            [Allow] => GET, HEAD
            [Date] => Mon, 20 Apr 2015 22:20:10 GMT
            [Server] => gws
            [Content-Length] => 1453
            [X-Frame-Options] => SAMEORIGIN
            [Alternate-Protocol] => 80:quic,p=1
            [Content-Type] => Array
                (
                    [0] => text/html
                    [charset] => UTF-8
                )

            [X-XSS-Protection] => Array
                (
                    [1] => 1
                    [mode] => block
                )

        )

    [body] => <!DOCTYPE html>...etc

)
up
-9
dongchao769390531 at 163 dot com
5 years ago
<?php
/**
* Curl send get request, support HTTPS protocol
* @param string $url The request url
* @param string $refer The request refer
* @param int $timeout The timeout seconds
* @return mixed
*/
function getRequest($url, $refer = "", $timeout = 10)
{
   
$ssl = stripos($url,'https://') === 0 ? true : false;
   
$curlObj = curl_init();
   
$options = [
       
CURLOPT_URL => $url,
       
CURLOPT_RETURNTRANSFER => 1,
       
CURLOPT_FOLLOWLOCATION => 1,
       
CURLOPT_AUTOREFERER => 1,
       
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
       
CURLOPT_TIMEOUT => $timeout,
       
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
       
CURLOPT_HTTPHEADER => ['Expect:'],
       
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
    ];
    if (
$refer) {
       
$options[CURLOPT_REFERER] = $refer;
    }
    if (
$ssl) {
       
//support https
       
$options[CURLOPT_SSL_VERIFYHOST] = false;
       
$options[CURLOPT_SSL_VERIFYPEER] = false;
    }
   
curl_setopt_array($curlObj, $options);
   
$returnData = curl_exec($curlObj);
    if (
curl_errno($curlObj)) {
       
//error message
       
$returnData = curl_error($curlObj);
    }
   
curl_close($curlObj);
    return
$returnData;
}

/**
* Curl send post request, support HTTPS protocol
* @param string $url The request url
* @param array $data The post data
* @param string $refer The request refer
* @param int $timeout The timeout seconds
* @param array $header The other request header
* @return mixed
*/
function postRequest($url, $data, $refer = "", $timeout = 10, $header = [])
{
   
$curlObj = curl_init();
   
$ssl = stripos($url,'https://') === 0 ? true : false;
   
$options = [
       
CURLOPT_URL => $url,
       
CURLOPT_RETURNTRANSFER => 1,
       
CURLOPT_POST => 1,
       
CURLOPT_POSTFIELDS => $data,
       
CURLOPT_FOLLOWLOCATION => 1,
       
CURLOPT_AUTOREFERER => 1,
       
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
       
CURLOPT_TIMEOUT => $timeout,
       
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
       
CURLOPT_HTTPHEADER => ['Expect:'],
       
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
       
CURLOPT_REFERER => $refer
   
];
    if (!empty(
$header)) {
       
$options[CURLOPT_HTTPHEADER] = $header;
    }
    if (
$refer) {
       
$options[CURLOPT_REFERER] = $refer;
    }
    if (
$ssl) {
       
//support https
       
$options[CURLOPT_SSL_VERIFYHOST] = false;
       
$options[CURLOPT_SSL_VERIFYPEER] = false;
    }
   
curl_setopt_array($curlObj, $options);
   
$returnData = curl_exec($curlObj);
    if (
curl_errno($curlObj)) {
       
//error message
       
$returnData = curl_error($curlObj);
    }
   
curl_close($curlObj);
    return
$returnData;
}

$getRes = getRequest("https://secure.php.net/");
echo
$getRes;//Get index page html of php.net

$postRes = postRequest("https://secure.php.net/",[]);
echo
$postRes;
up
-5
yann at ykweyer dot fr
5 years ago
If you need to send an array of files using CURL (typical case: POST variable to a REST API), having an array of CURLFiles in the POSTFIELD won't work. You have to set as many variables in your postfield than there are files to send:

<?php
// Won't work:
$postfields = array(
   
'files' => array(
        new
CURLFile($path1),
        new
CURLFile($path2),
       
//...
   
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

// Use this instead:
$postfields = array(
   
'files[0]' => new CURLFile($path1),
   
'files[1]' => new CURLFile($path2),
   
//...
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
?>
up
-11
mkucera66 at seznam dot cz
8 years ago
FYI cURL support (default enabled, ok) is prerequisite for Installation and Configuration of the Collection Extension of wiki portal.
up
-9
politikos at live dot com
4 years ago
I'm having problem to read this website.

https://jornalggn.com.br/noticia/moro-agora-levanta-sigilo-de-decisao-que-protege-delatores-de-orgaos-federais-0

Because of redirects and browser checking.
Any links from this https://jornalggn.com.br/

I'm receiving just this response:

"Checking your browser before accessing jornalggn.com.br.

This process is automatic. Your browser will redirect to your requested content shortly.

Please allow up to 5 seconds…"

Any tips, how to solve this using php_curl
Thanks in advance
--DJ
up
-14
cikemic at mailtrix dot net
4 years ago
After reading this post only one words comes out from my mouth that is "WoW". This post has helps me to acquire some new knowledge. So thanks for sharing your valuable ideas with us.
http://bestfrontandreardashcam.com/
up
-19
madhouse-network at hotmail dot com
11 years ago
curl does not seem to work with SSL TLS (FTPES)

It will throw the following error:

(1) Protocol ftpes not supported or disabled in libcurl
up
-28
arturo at midnightvip dot com
9 years ago
Hey I modified script for php 5. Also I add support server auth. and fixed some little bugs on the script.

<?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.midnightvip.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;
   }
}
?>
up
-58
puspenduseth at gmail dot com
4 years ago
After reading this post only one words comes out from my mouth that is "WoW". This post has helps me to acquire some new knowledge. So thanks for sharing your valuable ideas with us.
https://supremeten.com/best-wall-lights/
up
-50
krogerfeedback at gmail dot com
3 years ago
participate in the <a href="krogerfeedback">https://krogerfeedback.reviews</a> and win $5000 gift card.

官方地址:https://www.php.net/manual/en/book.curl.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3