经常会用到一些采集网上的资源,普通网站很好采,get_file_contents()/c_url(). 有的网站会有登陆后才能采集,需要带cookie请求获取(登陆网站相同方法),下面记录一下使用方法,供参考。
1 <?PHP 2 header('content-type:text/html;charset=utf8'); 3 define("COOKIE_PATH",dirname(__FILE__).'/cookie.txt'); 4 //获取COOKIE 5 function curl_get_cookie($url){ 6 $ch = curl_init($url); //初始化 7 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 8 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false); 9 curl_setopt($ch, CURLOPT_URL, $url); 10 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'); 11 curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分 12 curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //返回字符串,而非直接输出 13 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_PATH); //存储cookies 14 curl_exec($ch); 15 curl_close($ch); 16 } 17 18 #带COOKIE请求 19 function curl_cookie_request($url,$post){ 20 $ch = curl_init($url); 21 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 22 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false); 23 curl_setopt($ch, CURLOPT_URL, $url); 24 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'); 25 curl_setopt($ch, CURLOPT_HEADER, 0); 26 if($post) { 27 curl_setopt($ch, CURLOPT_POST, 1); 28 curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 29 } 30 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 31 'Content-Type: application/json', 32 'Content-Length: ' . strlen($post)) 33 ); 34 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 35 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_PATH); //使用上面获取的cookies 36 $response = curl_exec($ch); 37 curl_close($ch); 38 return $response; 39 }