我正在尝试制作一个卷曲帖子来上传我的应用程序,我不知道我做错了什么,但我在响应中不断收到 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"答案。
我希望这对某人有所帮助!
就我而言,上传正在工作,但它正在上传空白图像。这是最终让它工作的 PHP curl 代码:
$fileName = 'test.png';
$filePath = 'test.png';
$url = 'https://test.zendesk.com/api/v2/uploads.json?filename=' . urlencode($fileName);
$file = fopen($filePath, "r");
$mimeContentType = mime_content_type($file);
$size = filesize($filePath);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
curl_setopt($ch, CURLOPT_POST ,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filePath));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: {$mimeContentType}"));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_USERPWD, "test@test.com/token:XXXXXXXXXXXXXXXXXXX");
$output = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
fclose($file);
curl_close($ch);