简体   繁体   中英

How to shorten the Url for tweet on tweeter

I need to make an URL shorten for posting on tweeter using pro-grammatically .I find the Google API to shorten the url but in this API we need to register to get an API key.

Is there any other way to make an long URL to short URL using php.

Thanks in advance.any help will be appreciated.

Google API also has a method that doesn't require a key, you can access it via curl, eg:

$your_url = 'www.example.com/supermegalongurl.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/urlshortener/v1/url");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('longUrl' => $your_url)));
$response = curl_exec($ch);
curl_close($ch);

$shorturl = json_decode($response)->id;

echo $shorturl; // http://goo.gl/b1rwpU 

大多数API服务都将要求您注册密钥bit.ly也是如此。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM