php - 为什么 Bit.ly v4 API 返回 INVALID_CONTENT_TYPE_HEADER?

标签 php curl url-shortener bit.ly

我遇到了错误。我使用这个 curl :

$url = "https://api-ssl.bitly.com/v4/shorten";
$data = array('long_url' => 'http://www.google.com');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$header = array('Authorization: Bearer ffaaf96dd9e.........');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
curl_close($ch);
print($response);

我收到了这样的回复:

HTTP/2 406 
server: nginx
date: Sun, 27 Oct 2019 09:01:26 GMT
content-type: application/json
content-length: 41
strict-transport-security: max-age=31536000; includeSubDomains
x-xss-protection: 1; mode=blockFilter
x-content-type-options: nosniff
x-frame-options: DENY
content-security-policy: default-src 'none
via: 1.1 google
alt-svc: clear

{"message":"INVALID_CONTENT_TYPE_HEADER"}

我用谷歌搜索了这个错误,没有帮助。为什么我会收到此错误?我在编码时是否犯了任何错误,或者除了生成 token 外,我还应该在我的 bitly 帐户中做其他事情吗?

编辑:我用谷歌搜索并在 github 中找到了这个库:https://github.com/phplicengine/bitly 我添加这个是为了帮助来这里的人搜索同样的问题。

最佳答案

我发现问题是我必须将 json 格式的参数发送给 bitly,而不是数组格式。我还在 google 上搜索并在 github 的 php 中找到了这个有用的 bitly api 库: https://github.com/phplicengine/bitly

关于php - 为什么 Bit.ly v4 API 返回 INVALID_CONTENT_TYPE_HEADER?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578197/

相关文章:

PHP/MySQL 在 session 超时时更新数据库

curl - 检查本地是否安装了cURL?

X509_V_FLAG_PARTIAL_CHAIN 的 cURL 简单选项

java - 如何最高效的获取完整的URL地址?

c# - 无法加载文件或程序集 System.Threading.Tasks,版本 = 2.5.19.0

php - Ajax 调用不返回任何内容

php - 从 CodeIgniter URL 中删除 index.php

php - 如何使用 php 处理 htaccess 错误?

bash - 为什么在传递到 bash 时使用 -Lo- 和 curl?

php - 如何在 PHP 中获取缩短 URL 的真实地址?