PHP CURL模拟表单post提交文件,curl POST文件

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;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值