1、封装好的函数,传入对应的参数就可以了
<?php
//php使用curl模拟form表单提交数据
/*
* $post_data 提交的数据数组
* //要上传的本地文件地址"@F:/xampp/php/php.ini"上传时候,上传路径前面要有@符号
* eg. $furl = "@H:/htdocs/ssd.jpg";
$furl1 = "@H:/htdocs/jiyiji.php";
$post_data = array (
"name"=>'cqs',
"upload" => $furl,
"upload2"=>$furl1
);
*
* $sumbit_url 提交的url地址
* //CURLOPT_URL 是指提交到哪里?相当于表单里的“action”指定的路径
eg.$sumbit_url = "http://127.0.0.1/b.php";
*
* $http_url 访问来源地址
* //伪造网页来源地址,伪造来自百度的表单提交
* eg.$http_url="http://www.baidu.com";
*
*/
function curl_form($post_data,$sumbit_url,$http_url){
//初始化
$ch = curl_init();
//设置变量
curl_setopt($ch, CURLOPT_URL, $sumbit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//执行结果是否被返回,0是返回,1是不返回
curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示
curl_setopt($ch, CURLOPT_REFERER, $http_url);
//表单数据,是正规的表单设置值为非0
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);//设置curl执行超时时间最大是多少
//使用数组提供post数据时,CURL组件大概是为了兼容@filename这种上传文件的写法,
//默认把content_type设为了multipart/form-data。虽然对于大多数web服务器并
//没有影响,但是还是有少部分服务器不兼容。本文得出的结论是,在没有需要上传文件的
//情况下,尽量对post提交的数据进行http_build_query,然后发送出去,能实现更好的兼容性,更小的请求数据包。
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//执行并获取结果
$output = curl_exec($ch);
if($outopt === FALSE)
{
echo "<br/>","cUrl Error:".curl_error($ch);
}
// 释放cURL句柄
curl_close($ch);
}
$furl = "@H:/htdocs/aa.jpg";
$furl1 = "@H:/htdocs/jiyiji.php";
$post_data = array (
"name"=>'cqs',
"upload" => $furl
);
$sumbit_url = "http://127.0.0.1/b.php";
$http_url="http://www.baidu.com";
curl_form($post_data,$sumbit_url,$http_url);
?>
2、处理文件
<?php
echo "<pre>";
var_dump($_REQUEST);
var_dump($_FILES);
foreach($_FILES as $k=>$v)
{
$filename=$v['name'];//文件名
$extname=substr($filename,strrpos($filename,'.')+1);//获取后缀名
//设置上传路径
$savePath='H:/htdocs/cqs/curl_form/upload/'.date("Ymd",time()).'/';
if(!file_exists($savePath)) mkdir($savePath);
//以时间来命名上传的文件
$file_name = md5(strval(rand()).time()) . "." . $extname;
$newfilename=$savePath . $file_name;//新文件地址名称
//是否上传成功
if (! copy ( $v[tmp_name], $newfilename ) )
{
echo '上传失败';
exit;
}
}
?>
{
if (class_exists('\CURLFile'))//使用此判断是否存在curlfile类,不存在使用@方式,兼容Php各个版本
{
$map[$k]=new CURLFile($v['tmp_name']);//
} else {
$map[$k]='@'.$v['tmp_name']; //图片文件,PHP5.5废弃了’@’这种模式,采用下面的方式
}
}