如何使用 PHP Bitly v4 缩短 URL?

How to shorten URL using PHP Bitly v4?

我有这个用于 Bitly v3 的代码,它运行良好。

<?php
$login = 'login-code-here';
$api_key = 'api-key-here';
$long_url = 'https://whosebug.com/questions/ask';

$ch = curl_init('http://api.bitly.com/v3/shorten?login='.$login.'&apiKey='.$api_key.'&longUrl='.$long_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$res = json_decode($result, true);
echo $res['data']['url']; // bit.ly/2PcG3Fg
?>

但是,在较新的版本中如何做到这一点?上面的示例使用 API 密钥,但它已被弃用以支持 OAuth 请求。

如何使用 Bitly v4 缩短 URL?

获取通用访问令牌

转到您的 Bitly,单击 top-right 侧的汉堡菜单 > 设置 > 高级设置 > API 支持 > 单击 link 通用访问令牌。输入您的密码并生成通用令牌。这就是您将用于身份验证的内容。

请参阅 https://dev.bitly.com/v4_documentation.html 并查找 使用单个帐户的应用程序 部分。

根据 https://dev.bitly.com/v4/#section/Application-using-a-single-account,身份验证发生了一些变化。

How you authenticate to the Bitly API has changed with V4. Previously your authentication token would be provided as the access_token query parameter on each request. V4 instead requires that the token be provided as part of the Authorization header on each request.

代码

有关 Bitly 期望的信息,请参阅此文档 https://dev.bitly.com/v4/#operation/createFullBitlink

在 v4 中,您可以在 headers 中对每个请求使用通用令牌作为承载,如下所示:

<?php

$long_url = 'https://whosebug.com/questions/ask';
$apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
$genericAccessToken = 'your-token';

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

print_r($result);

请求

您发送的

JSON 将如下所示:

{"long_url":"https:\/\/whosebug.com\/questions\/ask"}

回应

{
   "created_at":"1970-01-01T00:00:00+0000",
   "id":"shortcode-link-id-here",
   "link":"shortcode-link-here",
   "custom_bitlinks":[

   ],
   "long_url":"https://whosebug.com/questions/ask",
   "archived":false,
   "tags":[

   ],
   "deeplinks":[

   ],
   "references":{
      "group":"group-link-here"
   }
}

编辑

评论中有人请求只查看简短的 link 输出。为此,只需像这样调整代码:

<?php
$long_url = 'https://whosebug.com/questions/ask';
$apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
$genericAccessToken = 'your-token';

$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';
}

结果(假设上面的文件是test.php)

php test.php

bit.ly/2ZbYD4Z

这是一个 PHP 包,您可以使用 https://packagist.org/packages/codehaveli/bitly-php

使用包的步骤:

第 1 步:

composer require codehaveli/bitly-php:dev-master --prefer-source 通过 composer 安装包 [如果没有安装从这里获取 https://getcomposer.org/]

第 2 步:

从 Bitly 添加访问令牌和组 GUID [此处指南:https://www.codehaveli.com/how-to-generate-bitly-oauth-access-token/]

<?php

require 'vendor/autoload.php';

use Codehaveli\Bitly;
use Codehaveli\Exceptions\BitlyErrorException;

// First setup your credentials provided by Bitly

$accessToken  = "ACCESS_TOKEN_FROM_BITLY";
$guid         = "GUID_FROM_BITLY";

Bitly::init($accessToken, $guid);

第 3 步:

在使用访问令牌和指南初始化后,只需从资源中调用方法 getUrl,就像使用 URL 会给你简短的 link.

<?php

use Codehaveli\Bitly;
use Codehaveli\Exceptions\BitlyErrorException;

$accessToken  = "ACCESS_TOKEN_FROM_BITLY";
$guid         = "GUID_FROM_BITLY";

Bitly::init($accessToken, $guid);

$link = Bitly::link();

try {

    $shortLink = $link->getUrl("https://whosebug.com/"); // Generated link

} catch (BitlyErrorException $e) {

    $code    = $e->getCode();
    $message = $e->getMessage();
}

注意:此包正在积极开发中。