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'
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_getinfo(, CURLINFO_HTTP_CODE) if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')
=, CURLOPT_URL, 'http://www.baidu.com', CURLOPT_HEADER, 1, CURLOPT_RETURNTRANSFER, 1 = curl_exec((); ?>
<?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();?>
curl_setopt($curl, CURLOPT_HEADER, 1);list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);
<?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();?>
curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, array('user'=>'test'));
<?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!