PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,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 // 创建一个新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);
[php]
view plain
copy // 创建一个新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 /** * 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
$user=$_POST[ stuno ];
$password=$_POST[ stupwd ];
$yzm=$_POST[ stuyzm ];
//echo $user;
//echo $password;
//echo $yzm;
$cookie_file = tempnam( ./temp , cookie );
$login_url = http://xk.shnu.edu.cn:82/index.aspx
$post_fields = array( cktime = 3153600 , txtUserID = $user, txtUserPwd = $password, txtValidateCode = $yzm);
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPTFOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_exec($ch);
curl_close($ch);
$url= http://xk.shnu.edu.cn:82/index.aspx
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPTFOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);
?
更详细的参数说明参考:http://cn2.php.net/curl_setopt
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/11595.html
clinuxphp相关文章
- PHP常见面试题_php面试常问面试题
- 【php】关闭PHP错误提示方法,防止错误信息泄露
- Linux上运行PHP的简易方法(linux如何运行php)
- PHP连接MySQL的实现方法(php怎么连接mysql)
- 数据库远程连接MySQL数据库:利用PHP实现简易方法(php远程mysql)
- 实现PHP程序Redis连接池的简单方法(redis连接池php)
- 数据PHP实现MySQL数据导出的实现方法(php导出mysql)
- 优雅式PHP配置MySQL数据库(php 配置 mysql)
- 善数据库性能用PHP连接MSSQL提升数据库性能(php连接mssql改)
- PHP读取MSSQL数据库:实现简单快捷的网页查询(php读取mssql网页)
- PHP访问MSSQL数据库的实现方法(php 读取 mssql)
- 用PHP访问本地MSSQL数据库的实现方法(php访问本地mssql)
- PHP编写的MSSQL查询:实现可能性无限(php写mssql查询)
- PHP连接MSSQL数据库发生错误解决方法(php mssql 错误)
- PHP与MSSQL的脚本技术编写更快捷的网站程序(php mssql 脚本)
- 基于Redis和PHP技术的投票系统优化(投票 redis php)
- 以Redis为中心 轻松实现PHP消息订阅(redis消息订阅php)
- php-accelerator网站加速PHP缓冲的方法
- PHP中防止直接访问或查看或下载config.php文件的方法
- php去除换行符的方法小结(PHP_EOL变量的使用)
- 图解找出PHP配置文件php.ini的路径的方法