5

更新:下面的代码对我有用。希望它可以帮助某人找出他们的问题。

在出现几个错误后,它有助于返回并查看 Apple News Developer 网站上所有可能的错误代码。查看代码中的特定错误编号并确定它可能有什么问题。

按照 Apple News Developer 网站上的示例进行操作。尽管它们含糊不清,但它们确实包含重要信息!

//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));
4

2 回答 2

2

您的内容类型标头应该是Content-Type: application/json,但授权的内容类型只是值 `application/json'。尝试在规范请求中使用它而不是完整的标头。

于 2018-03-07T14:27:42.130 回答
1

您的 Content-type 标头丢失,添加如下:

$headers[] = $Content_Type;
于 2018-03-06T21:39:59.470 回答