goo.gl shortener api
https://developers.google.com/url-shortener/v1/getting_started
OAuth 2.0で使えるようです。
or apikey
or 何も無し。
とりあえず。何も無しで使ってみます。
$api = ‘https://www.googleapis.com/urlshortener/v1/url';
$post_data = json_encode(array(‘longUrl’ => $url ));
curl_setopt($ch, CURLOPT_URL, $api );
curl_setopt($ch,CURLOPT_HTTPHEADER,array (“Content-Type: application/json” ));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 2);
$json = curl_exec($ch);
curl_close($ch);
jsonデータをpostするだけです。
{
“kind”: “urlshortener#url”,
“id”: “http://goo.gl/xSJmh”,
“longUrl”: “http://blog.bbtune.com/”
}
“kind”: “urlshortener#url”,
“id”: “http://goo.gl/xSJmh”,
“longUrl”: “http://blog.bbtune.com/”
}
無事、こんなデータが。
で戻すときは
Expand a short URL
https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/xSJmh
{
“kind”: “urlshortener#url”,
“id”: “http://goo.gl/xSJmh”,
“longUrl”: “http://blog.bbtune.com/”,
“status”: “OK”
}
“kind”: “urlshortener#url”,
“id”: “http://goo.gl/xSJmh”,
“longUrl”: “http://blog.bbtune.com/”,
“status”: “OK”
}