如何使用PHP Bitly v4缩短URL?

如何使用PHP Bitly v4缩短URL?,php,bit.ly,Php,Bit.ly,我为Bitlyv3编写了这段代码,它运行良好 <?php $login = 'login-code-here'; $api_key = 'api-key-here'; $long_url = 'https://stackoverflow.com/questions/ask'; $ch = curl_init('http://api.bitly.com/v3/shorten?login='.$login.'&apiKey='.$api_key.'&longUrl='.$lo

我为Bitlyv3编写了这段代码,它运行良好

<?php
$login = 'login-code-here';
$api_key = 'api-key-here';
$long_url = 'https://stackoverflow.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请求


如何使用Bitlyv4缩短URL?

获取通用访问令牌

转到您的Bitly,单击右上角的hamburger菜单>设置>高级设置>API支持>单击链接Generic Access Tokens。键入密码并生成通用令牌。这就是您将用于身份验证的内容

请参阅并查找使用单个帐户的应用程序部分

身份验证已根据更改而有所更改

您对Bitly API进行身份验证的方式在V4中发生了变化。以前,您的身份验证令牌将作为每个请求的访问令牌查询参数提供。V4要求在每个请求上提供令牌作为授权标头的一部分

代码

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

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

   ],
   "deeplinks":[

   ],
   "references":{
      "group":"group-link-here"
   }
}
有关Bitly期望的信息,请参阅此文档

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

<?php

$long_url = 'https://stackoverflow.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);
编辑 注释中有一个只查看短链接输出的请求。为了做到这一点,只需调整代码如下:

<?php
$long_url = 'https://stackoverflow.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';
}

这是一个可以使用的PHP包

使用软件包的步骤:

第1步:

composer需要codehaveli/bitly-php:dev-master——更喜欢使用source
通过composer安装软件包[如果未安装,请从此处获取]https://getcomposer.org/]

第二步:

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

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

快速查看提供的文档,似乎不错,您坚持什么?您好,如何仅打印位url?api url看起来不正确,不是吗:-此外,这要求在我编写答案时,该小组的工作良好。规格可能已经改变了。欢迎您使用最新的有效代码编写答案@gvanto@dijon我已经编辑了我的答案,展示了如何只输出短链接而不输出其他内容(只要链接是解码JSON的属性)@zedfoxus工作得很好,感谢添加最后一位。
<?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);
<?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://stackoverflow.com/"); // Generated link

} catch (BitlyErrorException $e) {

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