Google API - URL Shortener with PHP - 中文— it-swarm.cn

文章推薦指數: 80 %
投票人數:10人

Google API - URL Shortener with PHP. 我的代码如下。

URL缩短服务有效,但是当我插入 $POST 时它不会。

有谁知道如何修复这个我看代码? // This is the URL you want ... /home/中文/php/GoogleAPI-URLShortenerwithPHPGoogleAPI-URLShortenerwithPHP我的代码如下。

URL缩短服务有效,但是当我插入$POST时它不会。

有谁知道如何修复这个我看代码?//ThisistheURLyouwanttoshorten $longUrl='http://www.mysite.com/XXXXX/XX/$_POST['qrname']'; //GetAPIkeyfrom:http://code.google.com/apis/console/ $apiKey='MyAPIKey'; $postData=array('longUrl'=>$longUrl,'key'=>$apiKey); $jsonData=json_encode($postData); $curlObj=curl_init(); curl_setopt($curlObj,CURLOPT_URL,'https://www.googleapis.com/urlshortener/v1/url'); curl_setopt($curlObj,CURLOPT_RETURNTRANSFER,1); curl_setopt($curlObj,CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curlObj,CURLOPT_HEADER,0); curl_setopt($curlObj,CURLOPT_HTTPHEADER,array('Content-type:application/json')); curl_setopt($curlObj,CURLOPT_POST,1); curl_setopt($curlObj,CURLOPT_POSTFIELDS,$jsonData); $response=curl_exec($curlObj); //Changetheresponsejsonstringtoobject $json=json_decode($response); curl_close($curlObj); echo'ShortenedURLis:'.$json->id; phpurlcurlqr-code202012年10月25日ItsJoeTurner请尝试以下方式$longUrl='http://www.mysite.com/XXXXX/XX/'.$_POST['qrname'];以上将有效。

112012年10月25日Hackableweb$longUrl="http://www.xxxxxxx.com"; $postData=array('longUrl'=>$longUrl); $jsonData=json_encode($postData); //4 $curlObj=curl_init(); curl_setopt($curlObj,CURLOPT_URL,'https://www.googleapis.com/urlshortener/v1/url?key=yourappkey'); curl_setopt($curlObj,CURLOPT_RETURNTRANSFER,1); curl_setopt($curlObj,CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curlObj,CURLOPT_HEADER,0); curl_setopt($curlObj,CURLOPT_HTTPHEADER,array('Content-type:application/json')); curl_setopt($curlObj,CURLOPT_POST,1); curl_setopt($curlObj,CURLOPT_POSTFIELDS,$jsonData); //5 $response=curl_exec($curlObj); $json=json_decode($response); //echo"

";
//print_r($json);exit;
//6
curl_close($curlObj);

//7
if(isset($json->error)){
echo$json->error->message;
}else{
echo$json->id;
}
72017年4月20日debasish你有一把钥匙,但你没有正确使用它您应将其附加到网址,不要在帖子中发送密钥https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey
请检查https://developers.google.com/url-shortener/v1/url/insert62015年2月11日ursuleacv你在单引号之间传递php变量,所以它不会被解析。

在双引号之间传递它$longUrl="http://www.mysite.com/XXXXX/XX/$_POST['qrname']"; 或者像这样结合$longUrl='http://www.mysite.com/XXXXX/XX/'.$_POST['qrname']; 52013年10月20日MahakChoudhary没有足够的声誉点尚未发表评论,但我通过更换线路使其工作正常:echo'ShortenedURLis:'.$json->id; 有:$shortLink=get_object_vars($json); echo"ShortenedURLis:".$shortLink['id']; 它可能只是我的PHP安装,但原始行为我保留了500内部错误。

12014年1月22日mittra$longUrl,'key'=>$apiKey); $curlObj=curl_init(); $jsonData=json_encode($postData); curl_setopt($curlObj,CURLOPT_URL,'https://www.googleapis.com/urlshortener/v1/url?key='.$postData['key']); curl_setopt($curlObj,CURLOPT_RETURNTRANSFER,1); curl_setopt($curlObj,CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curlObj,CURLOPT_HEADER,0); curl_setopt($curlObj,CURLOPT_HTTPHEADER,array('Content-type:application/json')); curl_setopt($curlObj,CURLOPT_POST,1); curl_setopt($curlObj,CURLOPT_POSTFIELDS,$jsonData); $response=curl_exec($curlObj); $json=json_decode($response); curl_close($curlObj); if(isset($json->error)||$json==null){ return$longUrl;//retrunsameurlincaseoferrorornullresponse }else{ return$json->id;//returnshortedurl } } //usethisfunctionhere $longUrl='https://www.w3schools.com/'; echoshort_url($longUrl);//printshorturl //Ifyouwanttoreturnshorturltolongurlusebelowfunction functionlong_url($shortUrl){ $apiKey='***********';//putyourGOOGLEAPISHORTENINGKey $params=array('shortUrl'=>$shortUrl,'key'=>$apiKey,'projection'=>"ANALYTICS_CLICKS"); $final_url='https://www.googleapis.com/urlshortener/v1/url?'.http_build_query($params); $curlObj=curl_init($final_url); curl_setopt($curlObj,CURLOPT_RETURNTRANSFER,1); curl_setopt($curlObj,CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curlObj,CURLOPT_HEADER,0); curl_setopt($curlObj,CURLOPT_HTTPHEADER,array('Content-type:application/json')); $response=curl_exec($curlObj); $json=json_decode($response); curl_close($curlObj); if(isset($json->error)||$json==null){ return$shortUrl; }else{ return$json->longUrl; } } //FunctionUsehere echo"
";//Fornextline $shortUrl='';//puttheshorturlgeneratedfromabovefunction echolong_url($shortUrl);//getlongurl ?> 12018年4月13日MukeshKumar尝试使用此代码。

我在为我工作。

$api_key='YOUR_KEY'; $request_data=array( 'longUrl'=>'YOUR_LONG_URL' ); $curl_obj=curl_init(sprintf('%s/url?key=%s','https://www.googleapis.com/urlshortener/v1',$api_key)); curl_setopt($curl_obj,CURLOPT_RETURNTRANSFER,true); curl_setopt($curl_obj,CURLOPT_POST,true); curl_setopt($curl_obj,CURLOPT_HTTPHEADER,array('Content-type:application/json')); curl_setopt($curl_obj,CURLOPT_POSTFIELDS,json_encode($request_data)); curl_setopt($curl_obj,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($curl_obj,CURLOPT_SSL_VERIFYHOST,false); $response=curl_exec($curl_obj); $json=json_decode($response); curl_close($curl_obj); var_dump($json); die(); 02017年6月21日UWU_SANDUN替换$longUrl='http://www.example.com/XXXXX/XX/$_POST['qrname']';以下内容$longUrl='http://www.example.com/XXXXX/XX/{$_POST['qrname']}';02018年8月5日kylefardy获取完整的URLPHP如何从YouTubeAPI获取YouTube视频缩略图?PHP+curl,HTTPPOST示例代码?获取URL查询字符串参数从字符串中删除所有特殊字符如何安装ext-curl扩展名PHP7?调用未定义的函数curl_init()。

?PHP卷曲自定义标头能够PHPcURL在单个请求中检索响应标头和正文?如何使用PHP中的cURL连接Tor隐藏服务?卷曲错误60:SSL证书:无法获得本地颁发者证书PHP-SSL证书错误:无法获得本地颁发者证书GETURL参数PHPHTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA正常如何使用HTTP基本身份验证发出请求PHP卷曲?什么是PHP中的cURL?使用URL重写URLPHP设置卷曲的超时时间PHP如何启用curl,安装UbuntuLAMP堆栈?如何在Ubuntu16.04中安装php-curl


請為這篇文章評分?