2016-11-09 10 views
2

アップルニュースのAPIを使用して接続できますが、json形式でデータを送信することはできません。 私のコードは以下の通りです。 しかし、私はjsonファイルを添付しようとします。私は署名失敗メッセージを取得しています。アップルニュースAPI。 PHPコードで接続してjsonデータを送信する

<?php 
$channel_id = 'xxxxxxxxxxxxxxxxxx'; 
$api_key_id = 'xxxxxxxxxxxxxxx'; 
$api_key_secret = 'xxxxxxxxxxxxxxxx'; 
// use the correct values above  
$data = file_get_contents('article.json'); 
$Content_type="application/json"; 

$url = 'https://news-api.apple.com/channels/'.$channel_id; 
$date = gmdate('Y-m-d\TH:i:s\Z'); 
$canonical_request = 'GET'.$url.$date.$Content_type; 
$key = base64_decode($api_key_secret); 
$hashed = hash_hmac('sha256', $canonical_request,$key,true); 
$signature = base64_encode($hashed); 
echo $signature; 
//curl options 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL, $url); 

$headers = array(); 
$headers[] = "Authorization: HHMAC; key={$api_key_id}; signature={$signature}; date={$date}"; 

$headers[] = "Content-Type: application/json"; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
//curl_setopt($ch, CURLOPT_POST, true); 

//curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

//get result 
$server_output = curl_exec ($ch); 
curl_close ($ch); 

print_r(json_decode($server_output)); 

?> 

答えて

1

あなたが標準的な要求を構築する際に誤ってコンテンツタイプを含めている、GETリクエストを送信しているので。

これそして、あなたのために働く必要があります。参考のため

$canonical_request = 'GET'.$url.$date; 

を、Apple News API Reference: Setting Up MAC/HMAC Authenticationを参照してください。

  1. は次のバイト単位の連結として要求の標準的なバージョンを作成します。 :

    • HTTPメソッド(たとえば、GETまたはすべて大文字でPOST)
    • ISO 8601形式
  2. で要求

  3. 現在の日付の完全なURLリクエストがPOSTリクエストであり、それは実体が含まれている場合は、次のものがあります。

    • Content-Typeヘッダの値
    • エンティティの完全な内容
関連する問題