2017-07-20 61 views
1

我正在嘗試POST到此API。不幸的是,我無法通過身份驗證。正確地發佈到此API

$apiKey = 'mykey'; 
$username = 'myuid'; 
$userKey = 'myuserkey'; 
$url = 'https://api.thetvdb.com/login?'; 
$LoginQuery = array('apikey' => $apiKey, 
        'userkey' => $userKey, 
        'username' => $username 
        ); 
$postfields = json_encode($LoginQuery); 
$urlquery = $url . json_encode($LoginQuery); 
p_print("URL: " . $urlquery); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$json = curl_exec($ch); 
curl_close($ch); 
print_r($json); 

響應:

{ 
    "Error": "Bad Content-Type or charset, expected 'application/json'" 
} 

心不是什麼我張貼JSON?我怎麼做錯了?

回答

4

您需要設置Content-Typeapplication/json

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));