【问题标题】:Problem to shorten URL using PHP-Bitly v4使用 PHP-Bitly v4 缩短 URL 的问题
【发布时间】:2021-08-22 17:16:35
【问题描述】:

我正在尝试使用 bit.ly API V4 缩短 URL,但我总是遇到同样的错误,我无法获取短 URL。 有人能帮助我吗? 这是我使用的代码:

$long_url = 'https://estaticos.muyinteresante.es/media/cache/1140x_thumb/uploads/images/gallery/59bbb29c5bafe878503c9872/husky-siberiano-bosque.jpg';
$apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
$genericAccessToken = 'MyToken';

$data = array(
    'long_url' => $long_url
);
$payload = json_encode($data);

$header = array(
    'Authorization: Bearer ' . $genericAccessToken,
    'Content-Type: application/json',
    'Content-Length: ' . strlen($payload)
);

$ch = curl_init($apiv4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
$resultToJson = json_decode($result);

if (isset($resultToJson->link)) {
    echo $resultToJson->link;
}
else {
    echo 'Not found';
}

我总是得到“未找到”,有人知道为什么吗?

【问题讨论】:

  • 我刚刚复制并粘贴了您的代码,更改了访问令牌并运行了脚本。它可以 100% 正常工作,没有问题。 not found 错误是否可能是因为您没有正确调用自己的脚本?您将这个脚本保存在哪里以及如何访问它?

标签: php bitly


【解决方案1】:

关于与 Bit.ly V4 集成以及如何缩短 URL,我有一个非常详细的答案。

你可以找到链接here

【讨论】:

    猜你喜欢
    • 2019-09-05
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多