【问题标题】:POSTing properly to this API正确发布到此 API
【发布时间】:2017-07-20 03:26:15
【问题描述】:

我正在尝试发布到此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 吗?我怎么做错了?

【问题讨论】:

    标签: php api curl post


    【解决方案1】:

    您需要将Content-Type 设置为application/json

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-06
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 2017-12-08
      • 1970-01-01
      • 2011-04-09
      相关资源
      最近更新 更多