Home > Article > Backend Development > PHP curl get post request usage example sharing
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'If you need to return the Header header, add it yourself
curl_setopt($curl, CURLOPT_HEADER, 1);Determine Returned status code:
curl_getinfo(, CURLINFO_HTTP_CODE) if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')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((); ?>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 = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; const sURL = 'https://www.baidu.com'; const sCookie = 'fake if you want'; 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();?>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);Complete code:
<?php class getRequest { const sUA = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; const sURL = 'https://www.baidu.com'; const sCookie = 'fake if you want'; 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) == '200') { list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2); // } print_r($sHeader); print_r($sBody); // curl_close($curl); } }$foo = new getRequest();$foo->vInitRequest();?>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('user'=>'test'));The simple version is as follows:
<?php$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1);$aPostData = array( 'username' => 'test', .....); curl_setopt($curl, CURLOPT_POSTFIELDS, $aPostData);$sData = curl_exec($curl); curl_close($curl);var_dump($sData);?>
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!