PHP中使用CURL(六)
curl常用的几个例子
1、抓取无访问控制文件
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/mytest/phpinfo.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把这行注释掉的话,就会直接输出
$result=curl_exec($ch);
curl_close($ch);
?>
2、使用代理进行抓取
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://blog.snsgou.com");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, 125.21.23.6:8080);
//url_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');如果要密码的话,加上这个
$result=curl_exec($ch);
curl_close($ch);
?>
3、post数据后,抓取数据
<?php
$ch = curl_init();
$data = array('name' => 'test', 'sex' => 1);//array(1)
curl_setopt($ch, CURLOPT_URL, 'http://localhost/mytest/curl/userinfo.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
4、抓取一些有页面访问控制的页面
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://club-china");
curl_setopt($ch, CURLOPT_USERPWD, '[username]:[password]');//破解页面访问控制
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://club-china");
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
?>
5、模拟登录
<?php
function checkLogin($user, $password){
if (empty($user) || empty($password)){
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_REFERER, "http://mail.sina.com.cn/index.html");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
curl_setopt($ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "&logintype=uid&u=" . urlencode($user) . "&psw=" . $password);
$contents = curl_exec($ch);
curl_close($ch);
if (!preg_match("/Location: (.*)\\/cgi\\/index\\.php\\?check_time=(.*)\n/", $contents, $matches)){
return false;
}else{
return true;
}
}
define("USERAGENT", $_SERVER['HTTP_USER_AGENT']);
define("COOKIEJAR", tempnam("c:\windwos\temp", "cookie"));
define("TIMEOUT", 500);
echo checkLogin("username", "password");
?>
6、文件上传
<?php
/**
* @param string $target_url 上传目标地址
* @param string $filename 上传文件路径
* @param string $form_name 表单名称
*/
function curlUploadFile($target_url, $filename, $form_name) {
$upload_file = new CURLFile($filename);
$post_data = array(
$form_name => $upload_file
); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch);
curl_close($ch);
} $target_url = 'http://www.codean.net/notFound/test.php';
$filename = realpath("C:/Users/HelloWorld/Desktop/Images/1.jpg");
$form_name = 'file'; // 接收端使用$_FILES接受
curlUploadFile($target_url, $filename, $form_name);
?>
7、文件流上传
/*
* 第三种写法,使用PHP流发送
* @param string $target_url 上传目标地址
*/
function curlUploadFile($target_url) {
$fh = fopen('php://temp', 'rw+');
$string = 'Hello World';
fwrite($fh, $string);
rewind($fh); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch);
curl_close($ch);
}
$target_url = 'http://www.codean.net/notFound/test.php';
curlUploadFile($target_url); // 接收端取出流文件并保存
$putdata = fopen('php://input', 'r');
$fp = fopen('test.txt', 'w');
while ($data = fread($putdata, 1024)) {
fwrite($fp, $data);
}
fclose($fp);
fclose($putdata);
PHP中使用CURL(六)的更多相关文章
- php中的curl使用入门教程和常见用法实例
摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...
- PHP中使用CURL请求页面,使用fiddler进行抓包
在PHP中使用CURL访问页面: <?php $ch = curl_init('http://www.baidu.com'); curl_setopt($ch, CURLOPT_RETURNTR ...
- PHP中使用cURL实现Get和Post请求的方法
1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特 ...
- PHP中的CURL函数库
PHP中的CURL函数库(Client URL Library Function) curl_close — 关闭一个curl会话curl_copy_handle — 拷贝一个curl连接资源的所有内 ...
- PHP中使用cURL
1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性 ...
- 在PHP中使用CURL,“撩”服务器只需几行——php curl详细解析和常见大坑
在PHP中使用CURL,"撩"服务器只需几行--php curl详细解析和常见大坑 七夕啦,作为开发,妹子没得撩就"撩"下服务器吧,妹子有得撩的同学那就左拥妹子 ...
- PHP中使用CURL之php curl详细解析
在正式讲怎么用之前啊,先提一句,你得先在你的PHP环境中安装和启用curl模块,具体方式我就不讲了,不同系统不同安装方式,可以google查一下,或者查阅PHP官方的文档,还挺简单的. 1. 拿来先试 ...
- 在PHP中使用CURL,“撩”服务器只需几行
在PHP中使用CURL,“撩”服务器只需几行https://segmentfault.com/a/1190000006220620 七夕啦,作为开发,妹子没得撩就“撩”下服务器吧,妹子有得撩的同学那就 ...
- PHP中使用CURL实现GET和POST请求数据
PHP中使用CURL实现GET和POST请求 一.什么是CURL? cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 ...
- Error【0007】:zabbix中因为curl版本过低而无法发送邮件
1. 错误背景 在centos6.5上,源码部署zabbix最新版本zabbix-3.2.14.部署后之后,在配置邮件发送报警时出错 2. 错误提示 3. 原因分析 从网上检索的结果是说,系统中的cu ...
随机推荐
- Linux升级glibc
参考http://www.linuxidc.com/Linux/2015-04/116472.htm via 红孩儿你好 一.简介 glibc是gnu发布的libc库,即c运行库.glibc是linu ...
- 动作Action
/** * DelayTime延迟 * @param d Duration 延迟时间 */ auto delayTime = DelayTime::create(); sprite->runAc ...
- RockMongo 安装
1. yum install php-pecl-http php 2. yum install httpd 3. yum install php-devel 4. pecl install mongo ...
- git add -f
git add -f 添加已被 .gitignore 忽略的文件/文件夹
- ASP.NET用户控件操作ASPX页面
定义一个不含数据的事件处理方法 用户控件 public event EventHandler Click; protected void Button1_Click(object sender, Ev ...
- 2.1 Word 插入 smartart、图表
1.插入-smartart 2.插入后的图表中,左边可键入文字 3.按住TAB可以往后缩进 选中目标按住shift+TAB 可以往前缩进. 1.插入-图表 2.选择形状 3.会成功插入对比图表 以及出 ...
- iOS 打包上传AppStore相关(3)-iTunes相应配置以及使用蒲公英网站进行应用托管分发(链接/二维码)
上一篇讲到我们最终生成了一个格式为 .xcarchive 的文件(可以右键并Show in Finder)查看.本篇我们就进行最后的设置,打包上传.另外,还有一个小福利,那就是打测试包分发链接测试. ...
- 用myeclipse 创建maven项目时,生成的项目名中总是包含Maven Webapp
解决办法:新建Maven项目时,展开Advanced-Name template中选择[artifactId]即可
- Mysql基础知识整
web项目部署 Java项目使用的web服务器:Tomcat.weblogic.webshare.jetty Php.python使用的web服务器:nginx.apache 搭建环境过程: 部署.发 ...
- libev实现分析
libev是一个事件驱动库,底层是基于select.epoll.kqueue等I/O复用接口.所谓事件驱动库,就是用户定义一个事件以及改事件发生时调用的函数,该库会监听该事件,并在事件发生时调用相应的 ...