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);
&nbsp;curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
&nbsp;curl_setopt($ch, CURLOPT_PROXY, 125.21.23.6:8080);
&nbsp;//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(六)的更多相关文章

  1. php中的curl使用入门教程和常见用法实例

    摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...

  2. PHP中使用CURL请求页面,使用fiddler进行抓包

    在PHP中使用CURL访问页面: <?php $ch = curl_init('http://www.baidu.com'); curl_setopt($ch, CURLOPT_RETURNTR ...

  3. PHP中使用cURL实现Get和Post请求的方法

    1.cURL介绍  cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特 ...

  4. PHP中的CURL函数库

    PHP中的CURL函数库(Client URL Library Function) curl_close — 关闭一个curl会话curl_copy_handle — 拷贝一个curl连接资源的所有内 ...

  5. PHP中使用cURL

    1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性 ...

  6. 在PHP中使用CURL,“撩”服务器只需几行——php curl详细解析和常见大坑

    在PHP中使用CURL,"撩"服务器只需几行--php curl详细解析和常见大坑 七夕啦,作为开发,妹子没得撩就"撩"下服务器吧,妹子有得撩的同学那就左拥妹子 ...

  7. PHP中使用CURL之php curl详细解析

    在正式讲怎么用之前啊,先提一句,你得先在你的PHP环境中安装和启用curl模块,具体方式我就不讲了,不同系统不同安装方式,可以google查一下,或者查阅PHP官方的文档,还挺简单的. 1. 拿来先试 ...

  8. 在PHP中使用CURL,“撩”服务器只需几行

    在PHP中使用CURL,“撩”服务器只需几行https://segmentfault.com/a/1190000006220620 七夕啦,作为开发,妹子没得撩就“撩”下服务器吧,妹子有得撩的同学那就 ...

  9. PHP中使用CURL实现GET和POST请求数据

    PHP中使用CURL实现GET和POST请求 一.什么是CURL? cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 ...

  10. Error【0007】:zabbix中因为curl版本过低而无法发送邮件

    1. 错误背景 在centos6.5上,源码部署zabbix最新版本zabbix-3.2.14.部署后之后,在配置邮件发送报警时出错 2. 错误提示 3. 原因分析 从网上检索的结果是说,系统中的cu ...

随机推荐

  1. GNU/Linux下Freeplane的界面渲染问题

    如下图所示,思维导图软件Freeplane在GNU/Linux下默认的界面渲染效果是很差的,即便将Preferences → Appearance → Antialias设置为Antialias al ...

  2. android 控件注意点

    控件一:listview 问题一:当listview的item中存在按钮这种控件时 item点击不能响应问题? 解决方案:在item的自定义控件的最外层空间 上添加属性 android:descend ...

  3. size_t

    size_t在C语言中就有了.它是一种"整型"类型,里面保存的是一个整数,就像int, long那样.这种整数用来记录一个大小(size).size_t的全称应该是size typ ...

  4. 跑github上的Symfony项目遇到的问题2

    最近学习symfony框架,下载了大量的github上的别人的项目,要想跑起别人的项目,总结了以下几个步骤: 第一, 克隆一份代码; 第二,安装依赖,前提是安装了composer, 1:在你下载的项目 ...

  5. Alamofire4.0 在 CocoaPods无法更新的问题

    因为淘宝镜像已经不能使用,使用新的镜像升级ruby到最新状态 platform :ios, '9.0'use_frameworks! target '输入你的工程名字' do pod 'Alamofi ...

  6. iOS UIBezierPath类 介绍

      使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和 ...

  7. 搭建git代码服务器

    在代码管理中,通常需要使用版本管理工具,git就是一个不错的选择,这里简单罗列一下git服务器的搭建过程. 1. 安装git工具包 2. 初始化git库:在代码服务器上,通常只需要创建一个不含有工作目 ...

  8. 向多个会话窗口发送命令 -SecureCRT

    1.前提 一个服务可能部署在多台机器上,这时如果要查问题,最繁复的方法就是打开该服务的每个session,把命令在每一台机器上复制一下执行,找到相关的日志:还有一种方法就是一条命令同时向多个会话窗口发 ...

  9. Base algorithm

    今天介绍几种常见的算法,在面试中或许能派上用场 1.字符串倒转 //Reverse a string public static string Reverse(string ori) { string ...

  10. Openlayer 3 删除上一次绘制的图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...