更新:以下代码适合我。希望它能帮助某人找出问题所在。有几个错误后,返回并查看苹果新闻开发者网站上所有可能的错误代码帮助。查看代码中的特定错误号,并确定它可能出现的问题。按照苹果新闻开发者网站上的示例。即使它们含糊不清,但它们确实包含重要信息
//set the timezone
date_default_timezone_set('UTC');
//get json to be sent
$raw = file_get_contents('article.json');
$eol = "\r\n";
$data = '';
$bound= '535e329ca936f79a19ac9a251f7d48f7';
$data='--'.$bound.$eol.
"Content-Type: application/json" . $eol.
"Content-Disposition: form-data; name=metadata" . $eol. $eol.
'{
"data": {
"isCandidateToBeFeatured": "false",
"isSponsored": false,
"isPreview": true
}
}' .$eol.
'--'.$bound.$eol.
"Content-Type: application/json" . $eol.
"Content-Disposition: form-data; filename=article.json; name=article.json".$eol.$eol.
$raw.$eol.
'--'.$bound.'--'.$eol.$eol;
//set variables
$http_method = 'POST';
$date = gmdate('Y-m-d\TH:i:s\Z');
$key = 'xxx';
$url = 'https://news-api.apple.com/channels/xxx/articles';
$secret = 'xxx';
//cannonical request
$canonical_request = $http_method . $url . $date. 'multipart/form-data; boundary=535e329ca936f79a19ac9a251f7d48f7' . $data;
//Signature
$secretKey = base64_decode($secret);
$hash = hash_hmac('sha256', $canonical_request, $secretKey, true);
$signature = base64_encode($hash);
$authHeader = "HHMAC; key=$key; signature=$signature; date=$date;";
$headers = array();
$headers[] = "Authorization: $authHeader";
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: multipart/form-data; boundary=535e329ca936f79a19ac9a251f7d48f7";
$headers[] = "Content-Length: ".strlen($data);
//curl options
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//get result
$server_output = curl_exec ($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $status;
curl_close ($ch);
print_r(json_decode($server_output));
答案 0 :(得分:2)
您的内容类型标头应为Content-Type: application/json
,但授权的内容类型只是值application/json
尝试在规范请求中使用它而不是完整标头。
答案 1 :(得分:1)
您的Content-type标头丢失,请按以下方式添加:
$headers[] = $Content_Type;
答案 2 :(得分:0)
你如何修复MISSING [keyPath] =>数组([0] => article.json