web-dev-qa-db-fra.com

Google API - URL Shortener avec PHP

Mon code est ci-dessous. Le service de raccourcissement d'URL fonctionne, mais pas lorsque j'insère mon $POST. Est-ce que quelqu'un sait comment résoudre ce problème en regardant le code?

// 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;
20
ItsJoeTurner

Essayez comme ci-dessous

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

Ce qui précède fonctionnera.

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

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

Vous avez une clé, mais vous ne l'utilisez pas correctement

Vous devriez l'ajouter à l'URL, n'envoyez pas la clé par la poste

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

Veuillez vérifier https://developers.google.com/url-shortener/v1/url/insert

6
ursuleacv

vous passez la variable php entre les guillemets simples pour qu’elle ne soit pas analysée . 

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

OU concatinate comme ça 

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

Vous n'avez pas encore assez de points de réputation à commenter, mais j'ai réussi à le remplacer en remplaçant la ligne: 

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

avec: 

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

Ce pourrait être juste mon installation php, mais la ligne originale lançait toujours une erreur interne 500 pour moi.

1
mittra
<?php 
//URL Shortening Functions( Just copy & paste below code in your application)
function short_url($longUrl){
        $apiKey = '******************'; // put your GOOGLE API SHORTENING KEY 
        $postData = array('longUrl' => $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; // retrun same url in case of error or null response
        }else{
            return $json->id; // return shorted url
        }
    }
// use this function here
$longUrl = 'https://www.w3schools.com/';
echo short_url($longUrl); // print short url

// If you want to return short url to long url use below function
function long_url($shortUrl){
        $apiKey = '***********'; // put your GOOGLE API SHORTENING Key
        $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;
        }
    }
//Function Use here
echo "<br>"; // For next line
$shortUrl = ''; // put the short url generated from above function
echo long_url($shortUrl); // get long url
?>
1
Mukesh Kumar

Essayez avec ce code. Je travaille pour moi.

$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();
0
UWU_SANDUN

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

avec ce qui suit

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

0
kyle fardy