gpt4 book ai didi

php - 如何使用 PHP Bitly v4 缩短 URL?

转载 作者:可可西里 更新时间:2023-11-01 00:39:01 25 4
gpt4 key购买 nike

我有 Bitly v3 的代码,它运行良好。

<?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 key ,但它已被弃用以支持 OAuth 请求。

如何使用 Bitly v4 缩短 URL?

最佳答案

获取通用访问 token

转到您的 Bitly,单击右上角的汉堡菜单 > 设置 > 高级设置 > API 支持 > 单击链接通用访问 token 。输入您的密码并生成通用 token 。这就是您将用于身份验证的内容。

参见 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.

代码

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

在 v4 中,您可以像这样在每个请求的 header 中使用通用 token 作为承载:

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

请求

您发送的 JSON 将如下所示:

{"long_url":"https:\/\/st​​ackoverflow.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://stackoverflow.com/questions/ask",
"archived":false,
"tags":[

],
"deeplinks":[

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

编辑

评论中有要求只查看短链接输出。为此,只需像这样调整代码:

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

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

php test.php

bit.ly/2ZbYD4Z

关于php - 如何使用 PHP Bitly v4 缩短 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55681871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com