2016-02-25 22 views
0

我是PHP新手,我試圖通過基本身份驗證發送JSON請求。服務器與此錯誤迴應:PHP JSON使用基本身份驗證時出錯

object(stdClass)#1 (2) { ["error"]=> object(stdClass)#2 (2) { 
["code"]=> int(-32600) ["message"]=> string(44) "expected content type 
to be application/json" } ["jsonrpc"]=> string(3) "2.0" } 

從API文檔,這裏是請求格式:

Request Format: 

    { 
     "jsonrpc": "2.0", 
     "id":1, 
     "method": "acct.name.get", 
     "params": [ 
      "QrOxxEE9-fATtgAD" ] 
    } 

這裏是代碼...任何幫助將是巨大的 - 感謝

<?php 

$username = "username"; 
$password = "password"; 

$request = [ 
    'jsonrpc' => '2.0', 
    'id' => 1, 
    'method' => 'acct.name.get', 
    'params' =>['CA6ph0n7EicnDADS'], 
]; 

$curl_post_data = json_encode($request); 

$service_url = 'https://userapi.voicestar.com/api/jsonrpc/1'; 
$curl = curl_init($service_url); 
curl_setopt($curl, CURLOPT_URL, $service_url); 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
$curl_response = curl_exec($curl); 
$response = json_decode($curl_response); 
curl_close($curl); 

var_dump($response); 

?> 
+0

你爲什麼用'$ out = array_values($ request);''減少/重新索引你的數組?即使使用正確的標題,我也沒有看到此請求正常工作。 – Devon

+0

很誠實,我是一個完整的newbee - 我實際上試圖從API檢索數據(一個GET而不是POST-ing?) - 我嘗試了不同的東西來解決問題 – PiE

回答

1

其實,這是小事情......你的JSON /數組的格式不正確。在params結尾附加逗號可能實際上是問題。嘗試以下。

格式錯誤的數組將導致json_encode返回null。

<?php 

$username = "username"; 
$password = "password"; 

$request = [ 
    'jsonrpc' => '2.0', 
    'id'  => 1, 
    'method' => 'acct.name.get', 
    'params' => ['CA6ph0n7EicnDADS'] 
]; 

$curl_post_data = json_encode($request); 

$headers = ['Content-type: application/json']; 


$service_url = 'https://userapi.voicestar.com/api/jsonrpc/1'; 
$curl = curl_init($service_url); 
curl_setopt($curl, CURLOPT_URL, $service_url); 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
$curl_response = curl_exec($curl); 
$response = json_decode($curl_response); 
curl_close($curl); 

var_dump($response); 

?> 
+0

我T工作!真棒。謝謝你的時間。 – PiE

+0

絕對!請享用! –

0

在我看來,API正在尋找請求頭來反映JSON。

嘗試添加以下到您的選擇,看看返回

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json')); 
+0

謝謝 - 我試過這個,但我得到相同的錯誤... – PiE

+0

json_encode可以採取數組。關聯數組將被轉換爲正確的名稱:值對.... 轉換 $ curl_post_data = json_encode($ out); 到 $ curl_post_data = json_encode($ request); 並查看返回的內容 –

+0

同一事物 - 對象(stdClass)#1(2){[「error」] => object(stdClass)#2(2){[「code」] => int(-32600 )[「message」] => string(44)「預期內容類型爲application/json」} [「jsonrpc」] => string(3)「2.0」} – PiE