php post 失败,PHP Curl post上传图片文件失败问题

PHP使用cURL post 上传文件支持通过在数组数据中,使用“@+文件全路径”的语法附加文件,但是PHP从5.5开始引入了新的CURLFile类用来指向文件PHP推荐使用CURLFile替代旧的@语法。@语法在5.5已经被打了deprecated,在5.6中就直接被删除了。

$filepath='20170613114102-8016.jpg';

if (class_exists('\CURLFile')) {

// php 5.5 以后使用

$field = new \CURLFile(realpath($filepath));

} else {

//php 5.5前使用

$field = '@' . realpath($filepath);

}

$data=array(

'type'=>'recognize',

'image_path'=>$field,

'soft_id'=>'2540',

'soft_key'=>'10a1fda668b0a7d5eb59635483bf86c1',

'user_name'=>'test123',

'password'=>'test123',

'pic_type'=>'0004',

'timeout'=>60,

'remark'=>'',

'log'=>1

);

$url = 'http://www.zhima365.com/zmdemo_php/http_api.php';

$result=curlPost($url,$data);

var_dump($result);

function curlPost($uri, $data) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $uri);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_TIMEOUT, 10);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$return = curl_exec($ch);

curl_close($ch);

return $return;

}

?>

参考资料:

1.考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传

https://segmentfault.com/a/1190000000725185

2.Send file via cURL from form POST in PHP

https://stackoverflow.com/questions/4223977/send-file-via-curl-from-form-post-in-php

3.The CURLFile class

http://php.net/manual/en/class.curlfile.php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值