一、CURL小結
個人將歸納curl請求歸納為三步驟
## 個人將歸納curl請求歸納為三步驟
## 1、建立curl 句柄(curl_init),並設定為參數(curl_setop 1、建立curl 句柄(curl_init),並設定為參數(curl_setoptopp#.
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')判斷
=, 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();?>簡單版的GET請求如下,以下以請求百度為例,只設定了最基本的屬性:
curl_setopt($curl, CURLOPT_HEADER, 1);list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);
稍微複雜的設定了UA、Cookie等,https請求中才需要只用的SSL憑證校驗,http請求中可不用,如果需要請求有規律的位址,類似https:/ /example.com/?id=$i,修改for迴圈即可。
<?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);?>
#四、POST請求
POST請求無非比上述Get請求多設定了兩個參數。
以上是php之curl get post請求用法實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!