2017-10-12 3 views
0

認証されたHTTP GETリクエストを作成しようとしましたが、401 HTTPエラーが発生します。署名が正しいと確信しています。そのため、問題は18行目の複数のヘッダにあると思います。しかし、残念なことに正確な問題を見つけることはできません。PHP file_get_contentsにHTTPコード401が返されるUncomhorized

警告:file_get_contents(http://test.com/path):ストリームのオープンに失敗しました:HTTP要求に失敗しました。 HTTP/1.1のライン上の/home/host/path.phpで無断401 22

<?php 
$apiKey = '1'; 
$apiSecret = '2'; 

$verb = 'GET'; 

$path = '/path'; 
$nonce = '1'; 
$data = ''; 

$signature = hash_hmac('sha256', $verb . $path . $nonce . $data, $apiSecret); 

$url="http://test.com/path"; 

$opts = [ 
    "http" => [ 
     "method" => $verb, 
     "header" => "api-nonce: ".$nonce. "\r\n", "api-key: " .$apiKey. "\r\n", "api-signature: " . $signature 
    ] 
]; 
$context = stream_context_create($opts); 
$json = file_get_contents($url, false, $context); 

if($json){ 
    $data = @json_decode($json, TRUE); 

    print_r($data); 
} 
?> 

答えて

1

あなたは代わりにあなただけそれらを連結する必要があり、あなたはヘッダを区切るためにコンマを使用しライン18 に間違った形式を持っていました1つの文字列として:

"header" => "api-nonce: ".$nonce. "\r\n" . "api-key: " .$apiKey. "\r\n" . "api-signature: " . $signature . "\r\n" 
+0

ありがとう!今は完璧に動作します。 – Remco

関連する問題