curl方式POST表单,实现类似于模拟浏览器表单提交文件
主要作用:
1、开发APP接口,post文件
2、同一文件提交给多个接口
3、选择已有的图片二次提交审核
我的使用场景就是同一文件提交给多个接口。我先提交给自己服务器保存,然后用保存的文件提交给另一家公司进行内容审核。
$header = array('Content-Type'=>'application/x-www-form-urlencoded');
$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, 'http://www.yizhongcar.com/img.php');
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/img.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>'@D:\phpStudy\WWW\1.jpg',
);
if (class_exists('\CURLFile')) {
$field = array('fieldname' => new \CURLFile(realpath($filepath)));
$post = array(
"file_box"=>
new \CURLFile(realpath('
D:\phpStudy\WWW\1.jpg
')
,
);
} else {
$post = array(
"file_box"=>'@D:\phpStudy\WWW\1.jpg',
);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
echo $response;