本文主要对php的curl方法curl_setopt()函数案例进行介绍:1.抓取网页的简单案例;2.post数据案例
通过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?// 创建一个新curl资源 $ch = curl_init();  // 设置url和相应的选项 curl_setopt($ch, curlopt_url, "http://www.baidu.com/"); curl_setopt($ch, curlopt_header, false);  // 抓取url并把它传递给浏览器 curl_exec($ch); //关闭curl资源,并且释放系统资源 curl_close($ch);
2、post数据案例:
[php] view plain copy print?// 创建一个新curl资源 $ch = curl_init(); $data = 'phone='. urlencode($phone); // 设置url和相应的选项 curl_setopt($ch, curlopt_url, "http://www.post.com/"); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $data); // 抓取url并把它传递给浏览器 curl_exec($ch); //关闭curl资源,并且释放系统资源 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?/**    * make an http request    *    * @return string api results    * @ignore    */   function http($url, $method, $postfields = null, $headers = array()) {     $this->http_info = array();     $ci = curl_init();     /* curl settings */     curl_setopt($ci, curlopt_http_version, curl_http_version_1_0);//让curl自己判断使用哪个版本     curl_setopt($ci, curlopt_useragent, $this->useragent);//在http请求中包含一个"user-agent: "头的字符串。     curl_setopt($ci, curlopt_connecttimeout, $this->connecttimeout);//在发起连接前等待的时间,如果设置为0,则无限等待     curl_setopt($ci, curlopt_timeout, $this->timeout);//设置curl允许执行的最长秒数     curl_setopt($ci, curlopt_returntransfer, true);//返回原生的(raw)输出     curl_setopt($ci, curlopt_encoding, "");//http请求头中"accept-encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型。     curl_setopt($ci, curlopt_ssl_verifypeer, $this->ssl_verifypeer);//禁用后curl将终止从服务端进行验证     curl_setopt($ci, curlopt_headerfunction, array($this, 'getheader'));//第一个是curl的资源句柄,第二个是输出的header数据     curl_setopt($ci, curlopt_header, false);//启用时会将头文件的信息作为数据流输出     switch ($method) {       case 'post':         curl_setopt($ci, curlopt_post, true);         if (!empty($postfields)) {           curl_setopt($ci, curlopt_postfields, $postfields);           $this->postdata = $postfields;         }         break;       case 'delete':         curl_setopt($ci, curlopt_customrequest, 'delete');         if (!empty($postfields)) {           $url = "{$url}?{$postfields}";         }     }     if ( isset($this->access_token) && $this->access_token )       $headers[] = "authorization: oauth2 ".$this->access_token;     $headers[] = "api-remoteip: " . $_server['remote_addr'];     curl_setopt($ci, curlopt_url, $url );     curl_setopt($ci, curlopt_httpheader, $headers );     curl_setopt($ci, curlinfo_header_out, true );     $response = curl_exec($ci);     $this->http_code = curl_getinfo($ci, curlinfo_http_code);     $this->http_info = array_merge($this->http_info, curl_getinfo($ci));     $this->url = $url;     if ($this->debug) {       echo "=====post data======\r\n";       var_dump($postfields);        echo '=====info====='."\r\n";       print_r( curl_getinfo($ci) );        echo '=====$response====='."\r\n";       print_r( $response );     }     curl_close ($ci);     return $response;   }
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
php curl_init  与 curl_setopt函数
php curl_setopt函数用法介绍
curl_setopt函数用法讲解
以上就是php的curl方法curl_setopt()函数案例详解的详细内容。
   
 
   