Problem to shorten URL using PHP-Bitly v4

0 votes

I wanted to shorten URL using the bit.ly API V4, Here is the code that Im using:

$long_url = 'https://estaticos.muyinteresante.es/media/cache/1140x_thumb/uploads/images/gallery/59bbb29c5bafe878503c9872/husky-siberiano-bosque.jpg';
$apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
$genericAccessToken = 'MyToken';

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

But I keep getting this error in return. How can I solve this?

May 29, 2022 in PHP by Kichu
• 19,040 points
548 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

How to get base url in laravel using php?

Hello @kartik, You can use the URL facade ...READ MORE

answered Sep 17, 2020 in PHP by Niroj
• 82,840 points
17,068 views
0 votes
1 answer

How to check if a file exists from a url using php?

Hello, You have to use CURL function does_url_exists($url) { ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,840 points
4,079 views
0 votes
0 answers

How to pass a PHP variable using the URL

I need to pass some PHP variables ...READ MORE

Jun 19, 2022 in PHP by narikkadan
• 63,600 points
6,213 views
0 votes
1 answer

How to validate E-mail and URL of Php form?

hey, The code below shows a simple way ...READ MORE

answered Feb 13, 2020 in PHP by manish
2,363 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,357 views
0 votes
1 answer

How to send email using php?

Hello @kartik 1.) Download PHPMailer, open the zip file ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
1,364 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
1,561 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
11,401 views
0 votes
1 answer

Failure uploading Image on AmazonS3 with PHP SDK

Try this, I took it out from ...READ MORE

answered May 4, 2018 in AWS by Cloud gunner
• 4,670 points
4,444 views
0 votes
1 answer

Trying to call AWS API via PHP

Try using AWS SDK for PHP, Link ...READ MORE

answered Jun 6, 2018 in AWS by Cloud gunner
• 4,670 points
1,742 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP