2012-10-25 15 views
20

私のコードは以下の通りです。 URL短縮サービスは機能しますが、私が$POSTを挿入したときには機能しません。誰かがこのコードを見て修正する方法を知っていますか?Google API - URL短縮ツール

$longUrl = 'http://www.mysite.com/XXXXX/XX/'.$_POST['qrname'];

以下のように

// This is the URL you want to shorten 
$longUrl = 'http://www.mysite.com/XXXXX/XX/$_POST['qrname']'; 

// Get API key from : 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); 

// Change the response json string to object 
$json = json_decode($response); 

curl_close($curlObj); 

echo 'Shortened URL is: '.$json->id; 
+0

echo '短縮URLは:'です。$ json-> id;チェックの後に。 !できます 。 –

+1

このコードはどこにあるのかわかりませんが、APIドキュメントにありますが、ありがとう! – Macbernie

答えて

11

てみてください以上が動作します。

+8

あなたの問題はcURLに関するものではありません。PHPで変数と文字列を連結することです.http://php.net/manual/de/language.types.string.php –

+0

OPのPHPコードを使用できない理由が分かりますか? URLを短縮しますか?それは私にエラーメッセージ 'domain"を返します: "usageLimits"、 "reason": "dailyLimitExceededUnreg"、 "message": "Unauthenticated Useの毎日の制限を超えました。私は私のHttp Referrer、クォータ、すべてがうまくいくように見えるでしょう.... – Mavichow

5

php変数を単一引用符で渡しているため、解析されません。この

$longUrl = 'http://www.mysite.com/XXXXX/XX/'.$_POST['qrname']; 
+2

私はこの問題を解決するだけでなく、OPを伝えるので、私はこの答えをHackablewebの上に好むなぜ***! –

1

よう

$longUrl = "http://www.mysite.com/XXXXX/XX/$_POST['qrname']"; 

OR concatinateのような二重引用符の間 パス、それはコメントをまだ十分評判ポイントを持っていないが、私はこれはラインを置き換えることにより、正常に動作しました:

echo 'Shortened URL is: '.$json->id; 

と:

$shortLink = get_object_vars($json); 
echo "Shortened URL is: ".$shortLink['id']; 

これは私のPHPインストールだけかもしれませんが、元の行は私にとって500内部エラーを投げつけ続けました。

6
を確認してくださいあなたは鍵を持っていますが、あなたはあなたがポスト

https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey 

にキーを送信しない、URLに付加する必要があり、正しく

それを使用していません

$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 "<pre>"; 
// print_r($json);exit; 
    //6 
    curl_close($curlObj); 

    //7 
    if(isset($json->error)){ 
     echo $json->error->message; 
    }else{ 
     echo $json->id; 
    } 
0

このコードで試してください。私は私のために働いています。

$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(); 
関連する問題