一、CURL小結
個人將歸納curl請求總結成三步
1、建立curl 控制代碼(curl_init),並設定參數(curl_setopt)(開啟冰箱)
2、執行請求(curl_exec),處理返回的資料 (把大象塞進去)
3、關閉curl(curl_close),釋放所有資源(關上冰箱)
其實如果代碼看起來比較複雜,複雜的地方可能就是在處理返回資料的邏輯。
二、CURL_SETOPT
故名思議,SetOption 設定參數,其中囊括的參數較多,這裡只是簡單提取常用的幾個,如需查看更多參數,點擊這裡,常見的設定UA、Cookie、https等
bool curl_setopt ( , int , "User-Agent: ""Referer: " 禁止 cURL 驗證對等認證(peer'
如需返回的Header頭,自行添加
curl_setopt($curl, CURLOPT_HEADER, 1);
判斷返回的狀態代碼:
curl_getinfo(, CURLINFO_HTTP_CODE)if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')
簡單版的GET請求如下,下面以請求百度為例,只設定了最基本的屬性:
=, CURLOPT_URL, 'http://www.baidu.com', CURLOPT_HEADER, 1, CURLOPT_RETURNTRANSFER, 1 = curl_exec(();?>
稍微複雜的設定了UA、Cookie等,https請求中才需要只用的SSL認證校正,http請求中可不用,如果需要請求有規律的地址,類似https://example.com/?id=$i,修改for迴圈即可。
<?phpclass 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();?>
三、分離Response裡面的 Header和Body
首先要顯示Header資訊需要設定,如下設定即可取到header和body,當然還有其他方法大同小異
curl_setopt($curl, CURLOPT_HEADER, 1);list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);
完整代碼:
<?phpclass 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();?>
四、POST請求
POST請求無非比上述Get請求多設定了兩個參數。
1、嘿,我要用POST提交資料了。
2、我POST的資料的內容
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);?>