Đã lâu không viết bài chia sẽ code trên website. Hôm nay đang làm 1 mini project chia sẽ mã giảm giá chomienphi.vn mã khổ nỗi làm aff máy cái link nó siều dài, querystring từa lưa.
Trước giờ dùng Bitly trước tiếp ở website nó.
Mình có 1 đoạn script php cho anh em nào đang cần đề dùng tạo link cho website.
Đầu tiền vào site click vào link API Support: gốc phải trên cùng click vào.
- Chọn: SETTINGS => ADVANCED SETTINGS => API SUPPORT
- Hoặc click vào link này: https://app.bitly.com/Bj95fzErgUo/bitlinks/2Q2EQTw?actions=accountMain&actions=settings&actions=advancedSettings&actions=apiSupport&filterActive=
Lấy 2 thông tin LOGIN và API_KEY thay vào code bên dưới.
<?php
/**
* @author: nguyentinh
* @time: 10/31/19 10:43 AM
*
*/
class Bitly
{
public $login = 'LOGIN';
public $appkey = 'API_KEY';
public $version = '4';
public function generate()
{
$data = [
[
"title" => "Website mã giảm giá",
"aff_link" => "https://chomienphi.vn/search/adayroi/cong-nghe"
],
[
"title" => "Website rao vặt",
"aff_link" => "https://chomienphi.com.vn/category/bat-dong-san-MQ"
],
];
if (!empty($data)) {
foreach ($data as $key => $item) {
echo ($key + 1) . '/ ' . $item['title'] . PHP_EOL;
$shortLink = $this->createLink($item['aff_link']);
echo '==> Link: ' . $item['aff_link'] . PHP_EOL;
echo '==> Short Link: ' . $shortLink . PHP_EOL;
# xử lý save lại database nếu có.
}
}
}
/**
* @param string $url
* @return string
*/
public function createLink($url = '')
{
if (empty($url)) {
return '';
}
$query = 'version=' . $this->version;
$query .= '&longUrl=' . urlencode($url);
$query .= '&login=' . $this->login;
$query .= '&apiKey=' . $this->appkey;
$query .= '&format=' . 'json';
$bitly = 'https://api.bit.ly/shorten?' . $query;
$response = file_get_contents($bitly);
$json = @json_decode($response, true);
$rs = $json['results'][$url]['shortUrl'];
return $rs;
}
}
Sau đó mọi người qua trang https://app.bitly.com/ xem kết quả tạo link.
Chúc các bạn thành công, cùng nhau góp ý chia sẽ comment bên dưới nhé mọi người.