通过PHP Curl发布时返回的文件类型无效

时间:2016-06-16 03:52:24

标签: php image post curl

我使用CURL将图像发布到API。它完美地运作。但是当我对php做同样的事情时,有些问题无法解决。我尝试在php.ini文件中将 Date B126 H251 B126_log H251_log 1 2004/10/29 103.238 131.149 NA NA 2 2004/11/30 104.821 138.989 0.015 0.058 3 2004/12/31 105.141 137.266 0.003 -0.012 4 2005/0131 107.682 137.080 0.024 -0.001 设置为CURLOPT_SAFE_UPLOAD

API可以接受访问令牌作为URL参数和标题。

TRUE

以下是我的Php代码:

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"

我收到以下错误:

$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;

根据我的理解,图像验证失败。

1 个答案:

答案 0 :(得分:2)

尝试更改:

$post = array('title' => 'Via Api','image'=>'@'.$filename);

$post = array('title' => 'Via Api','image'=>'@/var/www/devel/'.$filename.';type=image/jpeg');

或者:

$post = array('title' => 'Via Api','image'=>'@'.$file_name_with_full_path.$filename.';type=image/jpeg');

查看完整正确的代码:

$filename = 'id-card-concept.jpg';
$post = array('title' => 'Via Api','file'=>'@/var/www/devel/'.$filename.';type=image/jpeg');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.site.com/v1/uploads?access_token=cfbf79449f6b71212b3983a49be0056dcb6cd0838c6904bdb9a8f461f9e04220");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;

有关function.curl-setopt的更多信息,请参阅