How to shorten product url using Bitly V4?

How to shorten product  url using Bitly V4?

Step 1 : create account in Bitly

go to profile settings >> Generic Access Token

Add your password and generate a generic token. That's what you'll use for authentication.

Step 2 : In php file :

if ($currentProduct = $block->getCurrentProduct())

{

$url=$currentProduct->getProductUrl();

}

$genericAccessToken = $this->bitlyKey(); //add your token key

$data = array(

'long_url' => $url //add url want to convert into shorten 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';

}

Result:

http://127.0.0.1/magento_2_3_5/endeavor-daytrip-backpack.html

bit.ly/2KtQUwl



Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.