CURL 是一個(gè)利用URL語(yǔ)法規(guī)定來(lái)傳輸文件和數(shù)據(jù)的工具,支持很多協(xié)議,如HTTP、FTP、TELNET等。最爽的是,php也支持 CURL 庫(kù)。使用PHP的CURL 庫(kù)可以簡(jiǎn)單和有效地去抓網(wǎng)頁(yè)。你只需要運(yùn)行一個(gè)腳本,然后分析一下你所抓取的網(wǎng)頁(yè),然后就可以以程序的方式得到你想要的數(shù)據(jù)了。無(wú)論是你想從從一個(gè)鏈接上取部分?jǐn)?shù)據(jù),或是取一個(gè)xml文件并把其導(dǎo)入數(shù)據(jù)庫(kù),那怕就是簡(jiǎn)單的獲取網(wǎng)頁(yè)內(nèi)容,CURL 是一個(gè)功能強(qiáng)大的PHP庫(kù)。
①:初始化
curl_init()
②:設(shè)置屬性
curl_setopt().有一長(zhǎng)串CURL 參數(shù)可供設(shè)置,它們能指定URL請(qǐng)求的各個(gè)細(xì)節(jié)。
③:執(zhí)行并獲取結(jié)果
curl_exec()
④:釋放句柄
curl_close()
①:GET方式實(shí)現(xiàn)

1 //初始化 2 $curl = curl_init(); 3 //設(shè)置抓取的url 4 curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); 5 //設(shè)置頭文件的信息作為數(shù)據(jù)流輸出 6 curl_setopt($curl, CURLOPT_HEADER, 1); 7 //設(shè)置獲取的信息以文件流的形式返回,而不是直接輸出。 8 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 9 //執(zhí)行命令10 $data = curl_exec($curl);11 //關(guān)閉URL請(qǐng)求12 curl_close($curl);13 //顯示獲得的數(shù)據(jù)14 PRint_r($data);②:POST方式實(shí)現(xiàn)
1 //初始化 2 $curl = curl_init(); 3 //設(shè)置抓取的url 4 curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); 5 //設(shè)置頭文件的信息作為數(shù)據(jù)流輸出 6 curl_setopt($curl, CURLOPT_HEADER, 1); 7 //設(shè)置獲取的信息以文件流的形式返回,而不是直接輸出。 8 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 9 //設(shè)置post方式提交10 curl_setopt($curl, CURLOPT_POST, 1);11 //設(shè)置post數(shù)據(jù)12 $post_data = array(13 "username" => "coder",14 "passWord" => "12345"15 );16 curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);17 //執(zhí)行命令18 $data = curl_exec($curl);19 //關(guān)閉URL請(qǐng)求20 curl_close($curl);21 //顯示獲得的數(shù)據(jù)22 print_r($data);③:如果獲得的數(shù)據(jù)時(shí)json格式的,使用json_decode函數(shù)解釋成數(shù)組。
$output_array = json_decode($data,true); //如果第二個(gè)參數(shù)為true,就轉(zhuǎn)為數(shù)組的形式。如果不填就為對(duì)象的形式
如果使用json_decode($data)解析的話(huà),將會(huì)得到object類(lèi)型的數(shù)據(jù)。
封裝的一個(gè)函數(shù)
1 //參數(shù)1:訪(fǎng)問(wèn)的URL,參數(shù)2:post數(shù)據(jù)(不填則為GET),參數(shù)3:提交的$cookies,參數(shù)4:是否返回$cookies 2 function curl_request($url,$post='',$cookie='', $returnCookie=0){ 3 $curl = curl_init(); 4 curl_setopt($curl, CURLOPT_URL, $url); 5 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'); 6 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 7 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); 8 curl_setopt($curl, CURLOPT_REFERER, "http://XXX"); 9 if($post) {10 curl_setopt($curl, CURLOPT_POST, 1);11 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));12 }13 if($cookie) {14 curl_setopt($curl, CURLOPT_COOKIE, $cookie);15 }16 curl_setopt($curl, CURLOPT_HEADER, $returnCookie);17 curl_setopt($curl, CURLOPT_TIMEOUT, 10);18 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);19 $data = curl_exec($curl);20 if (curl_errno($curl)) {21 return curl_error($curl);22 }23 curl_close($curl);24 if($returnCookie){25 list($header, $body) = explode("/r/n/r/n", $data, 2);26 preg_match_all("/Set/-Cookie:([^;]*);/", $header, $matches);27 $info['cookie'] = substr($matches[1][0], 1);28 $info['content'] = $body;29 return $info;30 }else{31 return $data;32 }33 }轉(zhuǎn)自:http://www.cnblogs.com/CHEUNGKAMING/p/5717429.html
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注