连续调用2个curl请求失败。 “需要长度”

时间:2012-08-18 16:53:13

标签: php curl

我正在尝试使用代码点火器在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“需要长度”,并且从打印出的信息中,请求的元数据似乎在第二个请求中不存在(即内容类型)。

以下是第一个请求的信息:

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

1 个答案:

答案 0 :(得分:0)

想出来。

替换:

$token =  $response[3];

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

明智的话(不是我,显然)始终关注你的空白。