使用 curl 上传应用包给出状态 500 - Zendesk REST API

Upload app package with curl gives status 500 - Zendesk REST API

我正在尝试 curl post 来上传我的应用程序,但我不知道我做错了什么,但我一直在响应中收到 500 错误,我不知道不知道该怎么办了...

// basic settings for your Zendesk
$userName = 'xxxxx@xxxx.com/token';
$apiKey = 'xkalkjsdXASJKDlasaknfkajsfASASASD';

// upload file info
$fileName = 'v5.0.zip';
$filePath = 'path/to/file/v5.0.zip';

$url = 'https://mysubdomain.zendesk.com/api/v2/apps/uploads.json';
$file = fopen($filePath, "r");
$size = filesize($filePath);



    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, $userName.":".$apiKey);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
    curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS
    curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array($fileName => "@".$filePath));
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
    $output = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    fclose($file);
    curl_close($ch);

我试过进入命令行,效果很好:

curl https://subdomain.zendesk.com/api/v2/apps/uploads.json \
  -F uploaded_data=@/path/to/file/v5.0.zip -X POST \
  -u "user/token:tokenpassword"

问题是,有人知道我可能做错了什么吗?我真的很绝望!

谢谢大家,

丹尼尔

所以,最后我找到了。

一切都正确,但内容类型不对,所以我更改了这一行:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));

至:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

正因为如此,curl 请求给了我一个 "NULL" 答案。

希望对大家有所帮助!