创建快捷方式使用谷歌API

需要缩短与PHP创建的链接。

注册后,请使用谷歌API。

 

使用方法:shorturl_google('原文地址“,”API密钥“);

 

<?php

function shorturl_google($url, $key)
{
$post = array('longUrl' => $url);
$json = json_encode($post);

$curlObj = curl_init();

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$key);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $json);

$json = json_decode(curl_exec($curlObj));

curl_close($curlObj);

return $json->id;
}

echo shorturl_google('http://kalmuri.cn.kilho.net/', 'API-KEY-API-KEY-API-KEY');

?>

arrow