Created
December 20, 2011 08:39
-
-
Save meeDamian/1500809 to your computer and use it in GitHub Desktop.
[ PHP | api | google ] goo.gl link shortenter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// add it to config file: | |
$google_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; | |
// add this to libs file: | |
function shorten($url) { | |
$gurl = "https://www.googleapis.com/urlshortener/v1/url?key=" . $GLOBALS['google_key']; | |
$url = json_encode(array("longUrl" => $url)); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_URL, $gurl); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $url); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: appliaction/json", "Content-Type: application/json")); | |
$r = json_decode(curl_exec($ch)); | |
return $r->id; | |
} | |
// example usage: | |
$short_url = shorten("https://gist.github.com/gists/1500809"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very cool, but
appliaction/json
should beapplication/json