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 ...
随机推荐
- hadoop(二)
三 Hive和Hbase #安装配置Hbase环境#主要参考https://my.oschina.net/zc741520/blog/388718网站配置的是集群,这里是伪分布,将网站中涉及多个主机的 ...
- mysql的联合,连接,子查询
- tcp异常终止连接
服务端: #include <sys/socket.h> #include <unistd.h> #include <sys/types.h> #include & ...
- PHP在浏览器上跟踪调试的方法以及使用ChromePhp、FirePHP的简单介绍
之前用ThinkPHP时发现有个 trace 函数可以跟踪调试,感觉很有意思,网上搜索了下类似的东西,发现了 ChromePhp ,以前没想过这样来调试 PHP 程序,感觉非常方便,很有用. Thin ...
- Chapter 2 Open Book——34
His gaze became appraising. "You put on a good show," he said slowly. 他的凝视变成了评价.“你上演了一场好戏” ...
- JavaScript忍者秘籍——原型
概要:本篇博客主要介绍JavaScript的原型 1.对象实例化 - 初始化的优先级 初始化操作的优先级如下: ● 通过原型给对象实例添加的属性 ● 在构造器函数内给对象实例添加的属性 在构造器内的绑 ...
- pgmpy包的安装,以及conda的安装
pgmpy包的安装,以及conda的安装 pgmpy的官方网站是这样说明安装过程的: 链接:Installation 共有三个步骤,第一步是安装依赖包 首先它讲述了在linux下使用pip安装的方法: ...
- foreach绑定
目的 foreach可以将一个数组中的实体循环的进行绑定.这在将一个list显示成table时非常有用. 假设数组是observable的,当在绑定后做了add, remove,或者重新排序后,绑定会 ...
- double hashing 双重哈希
二度哈希(rehashing / double hashing) 1.二度哈希的工作原理如下: 有一个包含多个哈希函数(H1……Hn)的集合.当我们要从哈希表中添加或获取元素时,首先使用哈希函数H1. ...
- echarts x轴或y轴文本字体颜色改变
1:x轴文本字体颜色改变 xAxis : [ { type : 'category', data : ['<30','30-','40-','50-','60-','>=70'], axi ...