Google API - URL Shortener with PHP

后端 未结 8 2027
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 12:24

My code is below. The URL shortening service works, but it doesn\'t when I insert my $POST. Does anyone know how to fix this my looking at the code?



        
相关标签:
8条回答
  • 2021-02-02 12:44

    you are passing the php variable between the single quotes so it will not be parsed. pass it between double quotes like

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

    OR concatinate like this

    $longUrl = 'http://www.mysite.com/XXXXX/XX/'.$_POST['qrname'];
    
    0 讨论(0)
  • 2021-02-02 12:49

    Try as below

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

    The above will work.

    0 讨论(0)
  • replace $longUrl = 'http://www.example.com/XXXXX/XX/$_POST['qrname']';

    with the following

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

    0 讨论(0)
  • 2021-02-02 12:52

    Don't have enough reputation points yet to comment, but I got this working fine by replacing the line:

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

    with:

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

    It could be just my php installation, but the original line kept throwing a 500 Internal Error for me.

    0 讨论(0)
  • 2021-02-02 12:52
    <?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
    ?>
    
    0 讨论(0)
  • 2021-02-02 12:53
    $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 讨论(0)
提交回复
热议问题