【发布时间】:2016-06-16 03:52:23
【问题描述】:
我正在使用 CURL 将图像发布到 API。它完美地工作。但是当我对 php 做同样的事情时,会出现一些无法解决的问题。我尝试在 php.ini 文件中将CURLOPT_SAFE_UPLOAD 设置为TRUE。
API 可以接受访问令牌作为 URL 参数和标头。
curl -v -F 'image=@/var/www/devel/id-card-concept.jpg;type=image/jpeg' -F 'title=test upload' -F 'description=test upload description' -H "Authorization: Bearer Access-Token" "https://api.site.com/v1/uploads"
以下是我的 PHP 代码:
$filename = 'id-card-concept.jpg';
$target_url = "https://api.site.com/v1/uploads?access_token=Access-Token";
$file_name_with_full_path = realpath('id-card-concept.jpg');
$post = array('title' => 'Via Api','image'=>'@'.$filename);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
我收到以下错误:
Array ( ) { "message": "Validation failed.", "errors": [ { "attribute": "image", "message": "is an invalid file type" } ] }
据我了解,该图像未通过验证。
【问题讨论】:
-
你的数据包嗅探器显示什么?