簡體   English   中英

PHP奇怪的代碼行為

[英]PHP Strange code behaviour

這是我的goo.gl網址縮短程序類。 像Googl :: shorten(“ http://google.com ”)一樣使用它。 我不明白為什么它返回Null。 我究竟做錯了什么?

<?php
define('GOOGLE_API_KEY', 'AIzaSyBS7WpEDiSZ91p-SJoNWOkKxqveb1sfpf4');
define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');
class Googl {

  static function shorten($longUrl)
    {
        // initialize the cURL connection
        $ch = curl_init(
            sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
        );

        // tell cURL to return the data rather than outputting it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // create the data to be encoded into JSON
        $requestData = array(
            'longUrl' => $longUrl
        );

        // change the request type to POST
        curl_setopt($ch, CURLOPT_POST, true);

        // set the form content type for JSON data
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

        // set the post body to encoded JSON data
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));

        // perform the request
        $result = curl_exec($ch);
        curl_close($ch);

        // decode and return the JSON response
        return json_decode($result, true);
    }

}

?>

您需要添加此參數。( 由於googleapis在HTTPs上運行

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

然后打電話

$res= Googl::shorten('http://stackoverflow.com');
var_dump($res);

OUTPUT :

array (size=3)
  'kind' => string 'urlshortener#url' (length=16)
  'id' => string 'http://goo.gl/Vmnf' (length=18)
  'longUrl' => string 'http://stackoverflow.com/' (length=25)

整個代碼..

<?php
define('GOOGLE_API_KEY', 'AIzaSyBS7WpEDiSZ91p-SJoNWOkKxqveb1sfpf4');
define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');
class Googl {

    static function shorten($longUrl)
    {
        // initialize the cURL connection
        $ch = curl_init(
            sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
        );

        // tell cURL to return the data rather than outputting it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // create the data to be encoded into JSON
        $requestData = array(
            'longUrl' => $longUrl
        );

        // change the request type to POST
        curl_setopt($ch, CURLOPT_POST, true);

        // set the form content type for JSON data
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // set the post body to encoded JSON data
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));

        // perform the request
        $result = curl_exec($ch);
        curl_close($ch);

        // decode and return the JSON response
        return json_decode($result, true);
    }

}
$res= Googl::shorten('http://stackoverflow.com');
var_dump($res);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM