PHP curl get post request usage example sharing

黄舟
Release: 2023-03-16 17:52:02
Original
2986 people have browsed it

1. Summary of CURL

I personally summarize the curl request into three steps

1. Create a curl handle (curl_init) and set the parameters (curl_setopt) (open the refrigerator)

  2. Execute the request (curl_exec), process the returned data (stuff the elephant in)

3. Close curl (curl_close), release all resources (close the refrigerator)

In fact, if the code looks complicated, the complexity may be in the logic of processing the returned data.

, CURL_SETOPT

Therefore, the name is thought, setOption sets parameters, of which there are more parameters. Here are just a few commonly used commonly used. If you need to view more parameters, click here, commonly, common Set UA, Cookie, https, etc.


##

bool curl_setopt          (   , int  ,  "User-Agent: ""Referer: " 禁止 cURL 验证对等证书(peer'
Copy after login

If you need to return the Header header, add it yourself


curl_setopt($curl, CURLOPT_HEADER, 1);
Copy after login

Determine Returned status code:


curl_getinfo(, CURLINFO_HTTP_CODE)
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')
Copy after login

The simple version of the GET request is as follows. Taking the request to Baidu as an example, only the most basic attributes are set:


 =, CURLOPT_URL, 'http://www.baidu.com', CURLOPT_HEADER, 1, CURLOPT_RETURNTRANSFER, 1 = curl_exec(();
?>
Copy after login

It is a little complicated to set up UA, Cookie, etc. Only SSL certificate verification is needed in https requests, but not in http requests. If you need to request a regular address, similar to https:/ /example.com/?id=$i, just modify the for loop.


<?php
class getRequest
{    
const sUA = &#39;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)&#39;;    
const sURL = &#39;https://www.baidu.com&#39;;    
const sCookie = &#39;fake if you want&#39;;    
function vInitRequest()
    {        $curl = curl_init();

        curl_setopt($curl, CURLOPT_HEADER, self::sUA);
        curl_setopt($curl, CURLOPT_COOKIE, self::sCookie);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        
        /*
         * ssl check,use for https url         
         */
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);//        
        for ($iId = 1; $iId < 1000; $iId++) {
//            $sURL = self::sURL.$iId;
        curl_setopt($curl, CURLOPT_URL, self::sURL);        
        $this->sExecRequest($curl);//        }    }    function sExecRequest($curl)
    {        $sRet = curl_exec($curl);        print_r($sRet);        /**
         * handle your response
         * stripos or preg         */
        curl_close($curl);
    }
}$foo = new getRequest();$foo->vInitRequest();?>
Copy after login

3. Separate the Header and Body in the Response

First, you need to set the Header information to display. You can get it by setting it as follows Header and body, of course, there are other methods that are similar


curl_setopt($curl, CURLOPT_HEADER, 1);list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);
Copy after login

Complete code:


<?php
class getRequest
{    
const sUA = &#39;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)&#39;;    
const sURL = &#39;https://www.baidu.com&#39;;    
const sCookie = &#39;fake if you want&#39;;    
function vInitRequest()
    {        
    $curl = curl_init();        
    $i = 0;
        curl_setopt($curl, CURLOPT_HEADER, self::sUA);
        curl_setopt($curl, CURLOPT_COOKIE, self::sCookie);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($curl, CURLOPT_URL, self::sURL);        
        $this->sExecRequest($curl);
    }    function sExecRequest($curl)
    {        $sRet = curl_exec($curl);        
    // if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == &#39;200&#39;) {
            list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);        
            // }
            print_r($sHeader);            
            print_r($sBody);        
            // curl_close($curl);    }
}$foo = new getRequest();$foo->vInitRequest();?>
Copy after login

4. POST request

The POST request simply sets two more parameters than the above Get request.

1. Hey, I want to submit data using POST.

2. The content of the data I POST


curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(&#39;user&#39;=>&#39;test&#39;));
Copy after login

The simple version is as follows:


<?php$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, &#39;http://www.baidu.com&#39;);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);$aPostData = array(  &#39;username&#39; => &#39;test&#39;,
   .....);
curl_setopt($curl, CURLOPT_POSTFIELDS, $aPostData);$sData = curl_exec($curl);
curl_close($curl);var_dump($sData);?>
Copy after login

The above is the detailed content of PHP curl get post request usage example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template