Joel G Mathew
Joel G Mathew

Reputation: 8081

POSTing properly to this API

I'm trying to POST to this API. Unfortunately, I'm not able to pass the authentication.

$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);

Response:

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

Isnt what I'm posting json? How am I doing it wrong?

Upvotes: 1

Views: 133

Answers (1)

Erik Godard
Erik Godard

Reputation: 6030

You need to set the Content-Type to be application/json

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

Upvotes: 4

Related Questions