这是我的 goo.gl url 缩短类。像 Googl::shorten(" http://google.com ") 一样使用它。我不明白为什么它返回 Null。我究竟做错了什么?
<?php
define('GOOGLE_API_KEY', 'AIzaSyBS7WpEDiSZ91p-SJoNWOkKxqveb1sfpf4');
define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');
class Googl {
static function shorten($longUrl)
{
// initialize the cURL connection
$ch = curl_init(
sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
);
// tell cURL to return the data rather than outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create the data to be encoded into JSON
$requestData = array(
'longUrl' => $longUrl
);
// change the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the form content type for JSON data
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
// set the post body to encoded JSON data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
// perform the request
$result = curl_exec($ch);
curl_close($ch);
// decode and return the JSON response
return json_decode($result, true);
}
}
?>