PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)

来源:互联网 发布:4g网络对讲机 编辑:程序博客网 时间:2024/04/30 05:24

通过curl_setopt()函数可以方便快捷的抓取网页(采集很方便大笑),curl_setopt 是php的一个扩展库

     使用条件:需要在php.ini 中配置开启。(PHP 4 >= 4.0.2)
       //取消下面的注释
extension=php_curl.dll

      在Linux下面,需要重新编译PHP了,编译时,你需要打开编译参数——在configure命令上加上“–with-curl” 参数。

1、 一个抓取网页的简单案例:

   

[php] view plain copy
 print?
  1. // 创建一个新cURL资源  
  2. $ch = curl_init();  
  3.   
  4. // 设置URL和相应的选项  
  5. curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/");  
  6. curl_setopt($ch, CURLOPT_HEADER, false);  
  7.   
  8. // 抓取URL并把它传递给浏览器  
  9. curl_exec($ch);  
  10.   
  11. //关闭cURL资源,并且释放系统资源  
  12. curl_close($ch);  
2、POST数据案例:

[php] view plain copy
 print?
  1. // 创建一个新cURL资源  
  2. $ch = curl_init();  
  3. $data = 'phone='. urlencode($phone);  
  4. // 设置URL和相应的选项  
  5. curl_setopt($ch, CURLOPT_URL, "http://www.post.com/");  
  6. curl_setopt($ch, CURLOPT_POST, 1);  
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  8. // 抓取URL并把它传递给浏览器  
  9. curl_exec($ch);  
  10.   
  11. //关闭cURL资源,并且释放系统资源  
  12. curl_close($ch);  

3、关于SSL和Cookie

关于SSL也就是HTTPS协议,你只需要把CURLOPT_URL连接中的http://变成https://就可以了。当然,还有一个参数叫CURLOPT_SSL_VERIFYHOST可以设置为验证站点。
关于Cookie,你需要了解下面三个参数:
CURLOPT_COOKIE,在当面的会话中设置一个cookie
CURLOPT_COOKIEJAR,当会话结束的时候保存一个Cookie
CURLOPT_COOKIEFILE,Cookie的文件。

PS:新浪微博登陆API部分截取(部分我增加了点注释,全当参数翻译下。哈哈) 有兴趣的自己研究,自己挪为己用。嘿嘿

[php] view plain copy
 print?
  1. /** 
  2.      * Make an HTTP request 
  3.      * 
  4.      * @return string API results 
  5.      * @ignore 
  6.      */  
  7.     function http($url$method$postfields = NULL, $headers = array()) {  
  8.         $this->http_info = array();  
  9.         $ci = curl_init();  
  10.         /* Curl settings */  
  11.         curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//让cURL自己判断使用哪个版本  
  12.         curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);//在HTTP请求中包含一个"User-Agent: "头的字符串。  
  13.         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);//在发起连接前等待的时间,如果设置为0,则无限等待  
  14.         curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);//设置cURL允许执行的最长秒数  
  15.         curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);//返回原生的(Raw)输出  
  16.         curl_setopt($ci, CURLOPT_ENCODING, "");//HTTP请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型。  
  17.         curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);//禁用后cURL将终止从服务端进行验证  
  18.         curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this'getHeader'));//第一个是cURL的资源句柄,第二个是输出的header数据  
  19.         curl_setopt($ci, CURLOPT_HEADER, FALSE);//启用时会将头文件的信息作为数据流输出  
  20.   
  21.         switch ($method) {  
  22.             case 'POST':  
  23.                 curl_setopt($ci, CURLOPT_POST, TRUE);  
  24.                 if (!empty($postfields)) {  
  25.                     curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);  
  26.                     $this->postdata = $postfields;  
  27.                 }  
  28.                 break;  
  29.             case 'DELETE':  
  30.                 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');  
  31.                 if (!empty($postfields)) {  
  32.                     $url = "{$url}?{$postfields}";  
  33.                 }  
  34.         }  
  35.   
  36.         if ( isset($this->access_token) && $this->access_token )  
  37.             $headers[] = "Authorization: OAuth2 ".$this->access_token;  
  38.   
  39.         $headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR'];  
  40.         curl_setopt($ci, CURLOPT_URL, $url );  
  41.         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );  
  42.         curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );  
  43.   
  44.         $response = curl_exec($ci);  
  45.         $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);  
  46.         $this->http_info = array_merge($this->http_info, curl_getinfo($ci));  
  47.         $this->url = $url;  
  48.   
  49.         if ($this->debug) {  
  50.             echo "=====post data======\r\n";  
  51.             var_dump($postfields);  
  52.   
  53.             echo '=====info====='."\r\n";  
  54.             print_r( curl_getinfo($ci) );  
  55.   
  56.             echo '=====$response====='."\r\n";  
  57.             print_r( $response );  
  58.         }  
  59.         curl_close ($ci);  
  60.         return $response;  
  61.     }  


更详细的参数说明参考:http://cn2.php.net/curl_setopt


转自:http://blog.csdn.net/liuxinmingcode/article/details/8043150


0 0
原创粉丝点击