curl_init函数用法
使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本,然后分析一下你所抓取的网 页,然后就可以以程序的方式得到你想要的数据了。无论是你想从从一个链接上取部分数据,或是取一个XML文件并把其导入数据库,那怕就是简单的获取网页内 容,cURL 是一个功能强大的PHP库。
PHP中的CURL函数库(Client URL Library Function)
curl_close — 关闭一个curl会话
curl_copy_handle — 拷贝一个curl连接资源的所有内容和参数
curl_errno — 返回一个包含当前会话错误信息的数字编号
curl_error — 返回一个包含当前会话错误信息的字符串
curl_exec — 执行一个curl会话
curl_getinfo — 获取一个curl连接资源句柄的信息
curl_init — 初始化一个curl会话
curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄资源
curl_multi_close — 关闭一个批处理句柄资源
curl_multi_exec — 解析一个curl批处理句柄
curl_multi_getcontent — 返回获取的输出的文本流
curl_multi_info_read — 获取当前解析的curl的相关传输信息
curl_multi_init — 初始化一个curl批处理句柄资源
curl_multi_remove_handle — 移除curl批处理句柄资源中的某个句柄资源
curl_multi_select — Get all the sockets associated with the cURL
extension, which can then be "selected"
curl_setopt_array — 以数组的形式为一个curl设置会话参数
curl_setopt — 为一个curl设置会话参数
curl_version — 获取curl相关的版本信息
curl_init()函数的作用初始化一个curl会话,curl_init()函数唯一的一个参数是可选的,表示一个url地址。
curl_exec()函数的作用是执行一个curl会话,唯一的参数是curl_init()函数返回的句柄。
curl_close()函数的作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。
例子一: 基本例子
基本例子
﹤?php
// 初始化一个 cURL 对象
$curl = curl_init();
// 设置你需要抓取的URL
curl_setopt($curl, CURLOPT_URL, 'http://www.cmx8.cn');
// 设置header
curl_setopt($curl, CURLOPT_HEADER, 1);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 运行cURL,请求网页
$data = curl_exec($curl);
// 关闭URL请求
curl_close($curl);
// 显示获得的数据
var_dump($data);
?>
例子二: POST数据
sendSMS.php,其可以接受两个表单域,一个是电话号码,一个是短信内容。
POST数据
﹤?php
$phoneNumber = '13812345678';
$message = 'This message was generated by curl and php';
$curlPost = 'pNUMBER=' . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.lxvoip.com/sendSMS.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec();
curl_close($ch);
?﹥
例子三:使用代理服务器
使用代理服务器
﹤?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.cmx8.cn');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, 'proxy.lxvoip.com:1080');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');
$data = curl_exec();
curl_close($ch);
?﹥
例子四: 模拟登录
Curl 模拟登录 discuz
程序,适合DZ7.0,将username改成你的用户名,userpass改成你的密码就可以了.
Curl 模拟登录 discuz 程序
<?php
!extension_loaded('curl') &&
die('The curl extension is not
loaded.');
$discuz_url =
'http://www.lxvoip.com';//论坛地址
$login_url = $discuz_url
.'/logging.php?action=login';//登录页地址
$get_url = $discuz_url .'/my.php?item=threads';
//我的帖子
$post_fields =
array();
//以下两项不需要修改
$post_fields['loginfield'] =
'username';
$post_fields['loginsubmit'] =
'true';
//用户名和密码,必须填写
$post_fields['username'] =
'lxvoip';
$post_fields['password'] =
'88888888';
//安全提问
$post_fields['questionid'] =
0;
$post_fields['answer'] =
'';
//@todo验证码
$post_fields['seccodeverify'] =
'';
//获取表单FORMHASH
$ch =
curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER,
0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,
1);
$contents =
curl_exec($ch);
curl_close($ch);
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i',
$contents,
$matches);
if(!empty($matches))
{
$formhash =
$matches[1];
} else
{
die('Not
found the
forumhash.');
}
//POST数据,获取COOKIE
$cookie_file = dirname(__FILE__) .
'/cookie.txt';
//$cookie_file =
tempnam('/tmp');
$ch =
curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER,
0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,
1);
curl_setopt($ch, CURLOPT_POST,
1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
$post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR,
$cookie_file);
curl_exec($ch);
curl_close($ch);
//带着上面得到的COOKIE获取需要登录后才能查看的页面内容
$ch =
curl_init($get_url);
curl_setopt($ch, CURLOPT_HEADER,
0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,
0);
curl_setopt($ch, CURLOPT_COOKIEFILE,
$cookie_file);
$contents =
curl_exec($ch);
curl_close($ch);
var_dump($contents);
//上传文件
$file = 'file'; //要上传的文件
$url = 'url';//target url
$fields['f'] = '@'.$file;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
curl_exec( $ch );
if ($error = curl_error($ch) ) {
die($error);
}
curl_close($ch);
这里说明一下CURLOPT_POSTFIELDS这个参数如果是POST字符串时,可以用形如“name=value&...”的字符串,如果post文件,就必须要用数组,并且文件名格式为"@绝对路径",这是 CURL 會幫你做 multipart/form-data 編碼。
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
//上传文件二
public function Action_Upload(){
$this->path_config();
exit();
$furl="@d:\develop\JMFrameworkWithDemo.rar";
$url= "http://localhost/DemoIndex/curl_pos/";
$this->upload_file_to_cdn($furl, $url);
}
public function upload_file_to_cdn($furl,$url){
// 初始化
$ch = curl_init();
// 要上传的本地文件地址"@F:/xampp/php/php.ini"上传时候,上传路径前面要有@符号
$post_data = array (
"upload" => $furl
);
//print_r($post_data);
//CURLOPT_URL 是指提交到哪里?相当于表单里的“action”指定的路径
//$url = "http://localhost/DemoIndex/curl_pos/";
// 设置变量
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//执行结果是否被返回,0是返回,1是不返回
curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示
//伪造网页来源地址,伪造来自百度的表单提交
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com");
//表单数据,是正规的表单设置值为非0
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);//设置curl执行超时时间最大是多少
//使用数组提供post数据时,CURL组件大概是为了兼容@filename这种上传文件的写法,
//默认把content_type设为了multipart/form-data。虽然对于大多数web服务器并
//没有影响,但是还是有少部分服务器不兼容。本文得出的结论是,在没有需要上传文件的
//情况下,尽量对post提交的数据进行http_build_query,然后发送出去,能实现更好的兼容性,更小的请求数据包。
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// 执行并获取结果
curl_exec($ch);
if(curl_exec($ch) === FALSE)
{
echo "<br/>"," cUrl Error:".curl_error($ch);
}
// 释放cURL句柄
curl_close($ch);
echo "aaa45";
}
function action_curl_pos(){
var_dump($_FILES);
$aa= move_uploaded_file($_FILES["upload"]["tmp_name"], "/wamp/tools/1.rar");
if($aa){
echo "11";
}
}
curl_init函数用法的更多相关文章
- php curl_init函数用法
原文地址:curl_init函数用法">php curl_init函数用法作者:loading 使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓 ...
- PHP中curl_init函数用法
使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网 页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并 ...
- php curl_init函数用法
使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网 页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并 ...
- (转载)php curl_init函数用法
(转载)http://blog.sina.com.cn/s/blog_640738130100tsig.html 使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所 ...
- php curl_init函数用法(http://blog.sina.com.cn/s/blog_640738130100tsig.html)
使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并把 ...
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- memcpy函数用法
memcpy函数用法 .分类: VC++ VC++ mfc matlab 2011-12-01 19:17 14538人阅读 评论(0) 收藏 举报 null 原型:extern void *memc ...
- Python回调函数用法实例详解
本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...
- win10 上运行 curl_init() 函数一直报错的解决办法
[问题现象] 1.把 APACHE 的 ZIP 包解压到目录,比如 d:\apache24\ 2.把 PHP 的 ZIP 包解压到目录,比如:d:\php56\ apache 与 php 与 MySQ ...
随机推荐
- #nav li:hover ul 与#nav li a:hover ul 的区别
#nav li:hover ul 与#nav li a:hover ul 有什么区别? ──────────────────────────────────────────── #nav li:hov ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- javax.servlet.jsp cannot be resolved to a type
参考链接 :http://www.tuicool.com/articles/7Njmqy
- webService 部署以后参数输入框不能显示
在<system.web> 标签下面加入这个 <system.web> <webServices> <protocols> <add name=& ...
- linux笔记四-------用户和组的管理
1.linux多用户.多任务操作系统 cat /etc/passwd //查看当前系统用户信息 cat /etc/group //查看当前系统组别信息 2.rbac:基于角色进行权限分配 用 ...
- c#中文转全拼或首拼
参考:http://www.jb51.net/article/42217.htmhttp://blog.csdn.net/cstester/article/details/4758172 Chines ...
- 【HDU4578 Transformation】线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 题意:有一个序列,有四种操作: 1:区间[l,r]内的数全部加c. 2:区间[l,r]内的数全部 ...
- NLP 自然语言处理
参考: 自然语言处理怎么最快入门:http://www.zhihu.com/question/ 自然语言处理简介:http://wenku.baidu.com/link?url=W6Mw1f-XN8s ...
- BizTalk开发系列(二) "Hello World" 程序搬运文件
我们在<QuickLearn BizTalk系列之"Hello World">里讲到了如何快速的开发第一个BizTalk 应用程序.现在我们来讲一下如何把这个程序改成用 ...
- IOS第八天(4:UITableViewController新浪微博, 代码创建布局和数据转模型)
******控制control #import "HMViewController.h" #import "HMStatus.h" #import " ...