array("ignore_errors" => true)); $context = stream_context_create($opts); if($shorturl) $path = $basepath."&shorturl=$shorturl&url=$url"; else $path = $basepath."&url=$url"; $response = @file_get_contents($path,false,$context); if(!isset($http_response_header)) { $result["errorMessage"] = "Local error: Failed to fetch API page"; return($result); } //Hacky way of getting the HTTP status code from the response headers if (!preg_match("{[0-9]{3}}",$http_response_header[0],$httpStatus)) { $result["errorMessage"] = "Local error: Failed to extract HTTP status from result request"; return($result); } $errorCode = -1; switch($httpStatus[0]) { case 200: $errorCode = 0; break; case 400: $errorCode = 1; break; case 406: $errorCode = 2; break; case 502: $errorCode = 3; break; case 503: $errorCode = 4; break; } if($errorCode==-1) { $result["errorMessage"] = "Local error: Unexpected response code received from server"; return($result); } $result["errorCode"] = $errorCode; if($errorCode==0) $result["shortURL"] = $response; else $result["errorMessage"] = $response; return($result); } //some example code using the function above $result = vgdShorten("https://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=louth&sll=53.800651,-4.064941&sspn=33.219383,38.803711&ie=UTF8&hq=&hnear=Louth,+United+Kingdom&z=14"); //below line would be how to request a custom URL instead of an automatically generated one //in this case asking for https://v.gd/mytesturl //$result = vgdShorten("https://www.reddit.com/","mytesturl"); if($result["shortURL"]) print("Success, your new shortened URL is ".$result["shortURL"]."\n"); else { print("There was an error, code: ".$result["errorCode"]."\n"); print($result["errorMessage"]."\n"); } if($result["errorCode"]==3) { //Error code 3 means your app has exceeded our rate limit. //In a real app you'd take some action here to prevent it //from using v.gd again for 1 minute or so. } ?>