关于curl上传文件
在localhost根目录建立1.php如下:
$ch=curl_init('http://localhost/post.php');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// localhost:8888是fiddler的代理,设置此选项用于让fiddler抓获post的请求
curl_setopt($ch, CURLOPT_PROXY, 'localhost:8888');
//下面这一句必须注释,不然Fiddler抓不到Post的http请求
//curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
//1.文件路径之前必须要加@
//2.文件路径带中文就会失败,例如'img_1'=>'@C:\Documents and Settings\Administrator\桌面\Android壁纸\androids.gif'
array('uname'=>'wqfghgfh','img_1'=>'@C:\Documents and Settings\Administrator\androids.gif')
);
$data=curl_exec($ch);
curl_close($ch);
echo $data;
?>
在localhost根目录新建post.php如下
var_dump($_POST);
var_dump($_FILES);
?>
访问http://localhost/1.php,返回如下内容:
array 'uname' => string 'wqfghgfh' (length=8) array 'img_1' => array 'name' => string 'androids.gif' (length=12) 'type' => string 'application/octet-stream' (length=24) 'tmp_name' => string 'C:\WINDOWS\Temp\php1BE.tmp' (length=26) 'error' => int 0 'size' => int 11293