我是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);
?>
你爲什麼用'$ out = array_values($ request);''減少/重新索引你的數組?即使使用正確的標題,我也沒有看到此請求正常工作。 – Devon
很誠實,我是一個完整的newbee - 我實際上試圖從API檢索數據(一個GET而不是POST-ing?) - 我嘗試了不同的東西來解決問題 – PiE