0

我正在尝试使用代码点火器在 PHP 中创建一个谷歌融合表。这是我的代码:

{
    $this->load->library('curl');

    $token_url = 'https://www.google.com/accounts/ClientLogin';

    //set username and password
    $email = "xxxx@gmail.com";
    $pass = "yyyyyy";

    $request_body = array("accountType" => "HOSTED_OR_GOOGLE",
                            "Email" => $email,
                            "Passwd" => $pass,
                            "service" => "fusiontables",
                            "source" => "fusiontables.GoogleFusion",
                        );

    $resp = $this->curl->simple_post($token_url, 
                                     $request_body, 
                                     array(CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'))
                                     );  
    $response = explode("=", $resp);

    $token =  $response[3];

    echo "token = ". $token . "<br>";

$url = "http://tables.googlelabs.com/api/query";
    //$token = 'DQBBBLcAAABtXliIIvSrim6JpcrxFF23EKVEJHlrDob2TICiFGUISmUd6UXN3Y19MqvYozPuY973kPafpVEReFR8geHHpNGPeO2PGLr8aqQa__v2rZLR4XB0WwoNb0ksa2WWgve2tocgkfwFY4OjQkcbSqUZJbxPBNk5vYGT6kL9hZNNJRZX-XYhUGfYiGlpNyWA0Pe2ql23PsUVRKWotYjgTIKOzO_p6zEAbaEkoxZkpsioXQQqMTM2QFf-hKjYcbOai80IAQI';

    print_r($this->curl->info);

    $post_data = array(
                        "sql"=>"CREATE TABLE 'table123' (word: STRING, geom: LOCATION, timestamp: DATETIME)",
                        );  

    $resp = $this->curl->simple_post(   $url,
                                        $post_data,
                                        array(CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded', "Authorization: GoogleLogin auth=" . $token))
                                    );

    echo "<br>";
    print_r($this->curl->info);
echo "<br>";
    print_r($resp);
}

我正在使用以下 curl 库 - https://github.com/philsturgeon/codeigniter-curl,而且奇怪的是,如果我取消注释硬编码的令牌行,整个事情就可以了。如果需要,我已经提供了信息,但底线是我收到错误 411“需要长度”,并且从打印出来的信息中,第二个请求中似乎没有请求的元数据(即 Content-Type)。

这是第一个请求的信息:

Array ( [url] => https://www.google.com/accounts/ClientLogin   
    [content_type] => text/plain   
    [http_code] => 200   
    [header_size] => 275   
    [request_size] => 268   
    [filetime] => -1   
    [ssl_verify_result] => 0   
    [redirect_count] => 0   
    [total_time] => 0.206787   
    [namelookup_time] => 4.3E-5   
    [connect_time] => 0.034345   
    [pretransfer_time] => 0.07166   
    [size_upload] => 124   
    [size_download] => 818   
    [speed_download] => 3955   
    [speed_upload] => 599   
    [download_content_length] => 818   
    [upload_content_length] => 0   
    [starttransfer_time] => 0.20675   
    [redirect_time] => 0   
    [certinfo] => Array ( )   
       )   

第二个,缺少元数据:

Array ( [url] => http://tables.googlelabs.com/api/query   
        [content_type] =>   
        [http_code] => 411   
        [header_size] => 0   
        [request_size] => 538   
        [filetime] => -1   
        [ssl_verify_result] => 0   
        [redirect_count] => 0   
        [total_time] => 0.107731   
        [namelookup_time] => 0.000849   
        [connect_time] => 0.054077   
        [pretransfer_time] => 0.054085   
        [size_upload] => 97   
        [size_download] => 0   
        [speed_download] => 0   
        [speed_upload] => 900   
        [download_content_length] => -1   
        [upload_content_length] => 0   
        [starttransfer_time] => 0.107711   
        [redirect_time] => 0   
        [certinfo] => Array ( )   
          )
4

1 回答 1

0

弄清楚了。

代替:

$token =  $response[3];

$token =  trim($response[3]);

对明智的人(不是我,很明显)说的话总是要注意你的空白。

于 2012-08-19T08:17:06.597 回答