2012-10-25 49 views
20

Mein Code ist unten. Der URL Verkürzungsdienst funktioniert, aber es funktioniert nicht, wenn ich meine einfüge. Kann jemand das beheben, wenn ich den Code anschaue? Google API - URL Shortener mit PHP

// 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 entfernen oder kommentieren 'Verkürzte URL ist:'. $ Json-> id; nach Überprüfung. ! Es klappt . –

+1

Ich weiß nicht, wo Sie diesen Code im API-Dokument finden, aber danke! – Macbernie

Antwort

11

Versuchen wie unten

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

Die oben arbeiten.

+8

Ihr Problem ist nichts über cURL, es geht um die Verkettung von Variablen und String in PHP, siehe http://php.net/manual/de/language.types.string.php –

+0

Darf ich wissen, warum ich OPs PHP-Code nicht verwenden kann und kürze die URL? Es gibt mir die Fehlermeldung "Domäne" zurück: "usageLimits", "Grund": "dailyLimitExceededUnreg", "Nachricht": "Tägliches Limit für nicht authentifizierte Verwendung überschritten. Fortgesetzte Nutzung erfordert Anmeldung. "'? Ich würde überprüfen, meine Http-Referrer, Quote, alles sieht gut aus .... – Mavichow

5

Sie übergeben die PHP-Variable zwischen den Hochkommas, so dass es nicht analysiert wird. es zwischen doppelte Anführungszeichen passieren wie

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

OR concatinate wie diese

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

Ich bevorzuge diese Antwort über Hackableweb's, da es nicht nur das Problem zu lösen, sondern sagt auch der OP, *** Warum ***! –

1

nicht genug Rufpunkte noch zu kommentieren haben, aber ich habe dies durch den Austausch der Linie feinen Arbeiten:

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

mit:

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

Es könnte nur meine PHP-Installation sein, aber die ursprüngliche Zeile warf einen 500 Internal Error für mich.

6

Sie haben einen Schlüssel, aber Sie verwenden sie nicht richtig

Sie sollten es an die URL anhängen, den Schlüssel nicht in der

+4

Dies löste es für mich, einige der vorhandenen Dokumentation ist veraltet. – MarcF

6
https://developers.google.com/url-shortener/v1/url/insert überprüfen
https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey 

Bitte

Post schicken
$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

Versuchen Sie es mit diesem Code. Ich arbeite für mich.

$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(); 
Verwandte Themen