Php 生成goo.gl在本地主机上不起作用
我试图在创建帖子时通过function.php动态生成goo.gl链接…但是没有使用在localhost上创建的链接更新该字段..我就是想不出问题所在Php 生成goo.gl在本地主机上不起作用,php,Php,我试图在创建帖子时通过function.php动态生成goo.gl链接…但是没有使用在localhost上创建的链接更新该字段..我就是想不出问题所在 function newlinks($post_id){ $permaurl=get_permalink($post_id); $shorturl=make_my_url($permaurl); update_post_meta('$post_id','shorturl',$shorturl) } add_a
function newlinks($post_id){
$permaurl=get_permalink($post_id);
$shorturl=make_my_url($permaurl);
update_post_meta('$post_id','shorturl',$shorturl)
}
add_action( 'save_post', 'newlinks' );
function make_my_url($myurl)
{
//got te key from https://console.developers.google.com/iam-admin/projects
$key = 'AIzBP0';
$googer = new GoogleURLAPI($key);
// Test: Shorten a URL
$shortDWName = $googer->shorten($myurl);
return $shortDWName; // returns the short url
}
// Declare the class
class GoogleUrlApi {
// Constructor
function GoogleURLAPI($key,$apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
// Keep the API Url
$this->apiURL = $apiURL.'?key='.$key;
}
// Shorten a URL
function shorten($url) {
// Send information along
$response = $this->send($url);
// Return the result
return isset($response['id']) ? $response['id'] : false;
}
// Send information to Google
function send($url,$shorten = true) {
// Create cURL
$ch = curl_init();
// If we're shortening a URL...
if($shorten) {
curl_setopt($ch,CURLOPT_URL,$this->apiURL);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
}
else {
curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// Execute the post
$result = curl_exec($ch);
// Close the connection
curl_close($ch);
// Return the result
return json_decode($result,true);
}
}
你的代码很好。我已经创建了自己的API密钥,并尝试了这个方法,效果很好。问题可能出在谷歌控制台上
请登录并尝试在那里生成短url。谷歌应该要求你授权你之前创建的密钥。之后,代码应该可以正常工作 您的代码很好。我已经创建了自己的API密钥,并尝试了这个方法,效果很好。问题可能出在谷歌控制台上
请登录并尝试在那里生成短url。谷歌应该要求你授权你之前创建的密钥。之后,代码应该可以正常工作 代码似乎可以在线工作,但不能在本地主机上工作……我如何将其升级到本地主机上工作……您在本地主机上进行了测试吗?我需要在php.ini中进行任何更改吗?我不这么认为。如果你的allow_url_fopen打开,那么它应该可以正常工作。它在服务器上工作,但不是我的localhost…一定有什么我不知道的关键是你使用了浏览器javascript或其他东西吗?代码似乎在线工作,但不是在localhost上…我如何将它工作在localhost上…你在localhost上测试了吗?我需要在我的测试中做任何更改吗ini?我不这么认为。如果你的allow\u url\u fopen打开了,那么它应该可以正常工作。它在服务器上工作,但不是我的本地主机…一定有什么东西我找不到钥匙了。你使用浏览器javascript还是其他什么?