如何将获取到的图片以二进制的方式post到某地址

ryanlaw1988 2016-02-17 11:25:19
最近在开发速卖通上传图片的功能,速卖通的api文档


文档上还有实例,以下是实例的代码
<?php
//读取文件
$filePath="C:/123.jpg";
$fh = fopen($filePath, "rb");
$data = fread($fh, filesize($filePath));
fclose($fh);


$appKey = '132345213';
$appSecret ='kG=43214321jds';
$accessToken = '53494330-4033-4aa4-adeb-b7fd7bb54807';
$fileName = '123.jpg';

//post地址
$upload_image_server = 'http://gw.api.alibaba.com/fileapi/param2/1/aliexpress.open/api.uploadImage/'.$appKey.'?access_token='.$accessToken.'&fileName='.$fileName;

//post提交
echo request_post($upload_image_server,$data);

// post数据到url的函数
function request_post($remote_server,$content){
$http_entity_type = 'application/x-www-from-urlencoded'; //发送的格式
$context = array(
'http'=>array(
'method'=>'POST',
// 这里可以增加其他header..
'header'=>"Content-type: " .$http_entity_type ."\r\n".
'Content-length: '.strlen($content),
'content'=>$content)
);
$stream_context = stream_context_create($context);
$data = file_get_contents($remote_server,FALSE,$stream_context);
return $data;
}
?>

但我就出现问题
Warning: file_get_contents(http://gw.api.alibaba.com/fileapi/param2/1/aliexpress.open/api.uploadImage/132345213?access_token=fe02dfd8-d3344-49c1-a123-951dbdd5c7cc&fileName=123.jpg): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in D:\wamp\www\express\demo.php on line 38

请有这方面的大神帮小弟看看是什么问题?

...全文
542 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
傲雪星枫 2016-02-18
  • 打赏
  • 举报
回复
改为curl请求并模拟user-agent试试,估计是要判断了用户user-agent
dy0312x 2016-02-17
  • 打赏
  • 举报
回复
Content-type: image/jpeg
-0000- 2016-02-17
  • 打赏
  • 举报
回复
// post数据到url的函数 function request_post($remote_server,$content){ $http_entity_type = multipart/form-data'; //发送的格式

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧