官方文档: https://dev.bitly.com/v4_documentation.html#section/Migrating-from-V3
1. 群组id的获取
GET https://api-ssl.bitly.com/v4/groups HTTP/1.1
Host: api-ssl.bitly.com
Authorization: Bearer {YOUR_ACCESS_TOKEN}
Accept: application/json
返回数据:
{
"groups": [
{
"created": "2019-08-31T03:08:54+0000",
"modified": "2019-08-31T03:08:54+0000",
"bsds": [],
"guid": "xxxxxxx",
"organization_guid": "xxxxxxx",
"name": "abensky",
"is_active": true,
"role": "org-admin",
"references": {
"organization": "https://api-ssl.bitly.com/v4/organizations/xxxxxxxx"
}
}
]
}
2. 缩短网址
POST https://api-ssl.bitly.com/v4/shorten HTTP/1.1
Host: api-ssl.bitly.com
Header设置:
Authorization: Bearer {YOUR_ACCESS_TOKEN}
Content-Type: application/json
body数据:
{"long_url": "https://www.takeyourtrip.com","group_guid": "前面获取到的群组groups.guid"}
返回结果示例 You will get a response that will contain values like:
{
"references": {
"property1": "string",
"property2": "string"
},
"archived": true,
"tags": [],
"created_at": "string",
"title": "string",
"deeplinks": [],
"created_by": "string",
"long_url": "string",
"custom_bitlinks": [],
"link": "string",
"id": "string"
}
实际返回数据:
{
"created_at": "2019-08-31T03:13:46+0000",
"id": "bit.ly/2ZL5Ood",
"link": "http://bit.ly/2ZL5Ood",
"custom_bitlinks": [],
"long_url": "https://www.takeyourtrip.com/",
"archived": false,
"tags": [],
"deeplinks": [],
"references": {
"group": "https://api-ssl.bitly.com/v4/groups/xxxxxx"
}
}
错误返回信息示例:
{"message": "FORBIDDEN","resource": "bitlinks", "description": "You are currently forbidden to access this resource."}
{"message":"INVALID_ARG_LONG_URL","resource":"bitlinks","description":"The value provided is invalid.","errors":[{"field":"long_url","error_code":"invalid"}]}
经过测试, group_guid 参数可以不带! 或许是因为个人版只有一个群组.
附录代码
<?php
// https://dev.bitly.com/ 国外 bitly 短网址api
// author: aben
// create date: 2020-2-22
Class bitlyApi {
//The URI of the standard bitly v4 API.
const BITLY_API_V4 = 'https://api-ssl.bitly.com/v4/';
private $token = '';
private $group_guid = '';
function __construct() {
$this->token = 'bilty_token';//put your token here
$this->group_guid = 'bilty_group_guid';//put one group id here
}
function __destruct() {
}
/**
* 获取短网址, 返回数组
* @param string $long_url 要转换的长网址
* @param string $token api使用的token
* @return array
*/
function get_short_url($long_url, $token = null, $group_guid = null) {
if ($token === null || strlen($token) == 0) {
$token = $this->token;
}
if ($group_guid === null || strlen($group_guid) == 0) {
$group_guid = $this->group_guid;
}
if (empty($token) || empty($group_guid)) {
return array('err_code' => 'error', 'err_msg' => 'token/group not set', 'short_url' => '');
}
$params = [];
$params['access_token'] = $token;
$params['longUrl'] = $long_url;
$params['group_guid'] = $group_guid;//v4
$rt = $this->bitly_get_v4('shorten', $params);
//返回结果示例
// {"created_at": "2019-08-31T03:13:46+0000","id": "bit.ly/2ZL5Ood","link": "http://bit.ly/2ZL5Ood","custom_bitlinks": [],"long_url": "https://www.takeyourtrip.com/","archived": false,"tags": [],"deeplinks": [],"references": {"group": "https://api-ssl.bitly.com/v4/groups/xxxx"}}
// {"message": "FORBIDDEN","resource": "bitlinks","description": "You are currently forbidden to access this resource."}
if (!is_array($rt)) {
return array('err_code' => 'error', 'err_msg' => 'request failed', 'short_url' => '');
}
if (!array_key_exists('id', $rt)) {
if (array_key_exists('message', $rt)) {
$err_msg = 'request failed: ' . $rt['message'];
} else {
$err_msg = 'request failed';
}
return array('err_code' => 'error', 'err_msg' => $err_msg, 'short_url' => '');
}
$err_code = 'success';
$err_msg = 'success';
$long_url = $rt['long_url'];
$short_url = $rt['id'];
return array('err_code' => $err_code, 'err_msg' => $err_msg, 'long_url' => $long_url, 'short_url' => $short_url);
}
function bitly_get_v4($endpoint, $params) {
/*
POST https://api-ssl.bitly.com/v4/shorten HTTP/1.1
Host: api-ssl.bitly.com
Header设置:
Authorization: Bearer ACCESS_TOKEN
Content-Type: application/json
body数据:
{"long_url": "https://www.takeyourtrip.com/","group_guid": "xxxx"}
实际返回数据:
{
"created_at": "2019-08-31T03:13:46+0000",
"id": "bit.ly/2ZL5Ood",
"link": "http://bit.ly/2ZL5Ood",
"custom_bitlinks": [],
"long_url": "https://www.takeyourtrip.com/",
"archived": false,
"tags": [],
"deeplinks": [],
"references": {
"group": "https://api-ssl.bitly.com/v4/groups/xxxx"
}
}
*/
$url = self::BITLY_API_V4 . $endpoint;
$header = [
'Authorization: Bearer ' . $params['access_token'],
'Content-Type: application/json'
];
$data = [
'long_url' => $params['longUrl'],
'group_guid' => $params['group_guid']
];
$rt = $this->curl_post($url, $header, $data);
//返回结果示例
// {"created_at": "2019-08-31T03:13:46+0000","id": "bit.ly/2ZL5Ood","link": "http://bit.ly/2ZL5Ood","custom_bitlinks": [],"long_url": "https://www.takeyourtrip.com/","archived": false,"tags": [],"deeplinks": [],"references": {"group": "https://api-ssl.bitly.com/v4/groups/xxxx"}}
// {"message": "FORBIDDEN","resource": "bitlinks","description": "You are currently forbidden to access this resource."}
$result = json_decode($rt, true);
return $result;
}
public function curl_post($url, $header = null, $data = null) {
$output = '';
//$header = ['Authorization: Bearer ACCESS_TOKEN', 'Content-Type: application/json'];
//$data = ['long_url'=>'https://www.takeyourtrip.com', 'group_guid'=>'GUID'];
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);//post
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0);//返回response头部信息
}
// 把post的变量加上
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$output = curl_exec($ch);
curl_close($ch);
} catch (\Exception $e) {
//tep_error_log('Bitly request failed, url: '.$url);
}
//状态返回200的才是正常
return $output;
}
}