【问题标题】:Google API - URL Shortener with PHPGoogle API - 使用 PHP 的 URL 缩短器
【发布时间】:2012-10-25 10:47:50
【问题描述】:

我的代码如下。 URL 缩短服务有效,但当我插入我的$POST 时它不起作用。有谁知道如何通过查看代码来解决这个问题?

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

【问题讨论】:

  • 删除或评论 echo 'Shortened URL is: '.$json->id;检查后。 !它有效。
  • 我不知道你在 API 文档中哪里可以找到这段代码,但是谢谢!

标签: php url curl qr-code


【解决方案1】:

尝试如下

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

以上方法都可以。

【讨论】:

  • 你的问题与 cURL 无关,它是关于在 PHP 中连接变量和字符串,请参阅php.net/manual/de/language.types.string.php
  • 我可以知道为什么我不能使用 OP 的 Php 代码并缩短 url 吗?它返回给我错误消息domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."?我会检查我的 Http Referrer,配额,一切看起来都不错......
【解决方案2】:
$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;
    }   

【讨论】:

    【解决方案3】:

    您在单引号之间传递了 php 变量,因此它不会被解析。 在像

    这样的双引号之间传递它
    $longUrl = "http://www.mysite.com/XXXXX/XX/$_POST['qrname']";
    

    或像这样连接

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

    【讨论】:

    • 我更喜欢这个答案而不是 Hackableweb 的答案,因为它不仅解决了问题,还告诉 OP,为什么
    【解决方案4】:

    你有一把钥匙,但你没有正确使用它

    你应该将它附加到网址中,不要在帖子中发送密钥

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

    请查看https://developers.google.com/url-shortener/v1/url/insert

    【讨论】:

    • 这为我解决了,一些现有的文档已经过时了。
    【解决方案5】:

    还没有足够的声望点来评论,但我通过替换以下行得到了这个工作正常:

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

    与:

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

    这可能只是我的 php 安装,但原行一直为我抛出 500 内部错误。

    【讨论】:

      【解决方案6】:
      <?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
      ?>
      

      【讨论】:

        【解决方案7】:

        试试这个代码。我为我工作。

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

        【讨论】:

          【解决方案8】:

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

          以下

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多