0

Google から何の応答も得られません。リクエストは 99% の確率で成功すると思いますが、最近、リクエストが成功しないことに気付き始めました。失敗を示す応答が返されないため、失敗しません。HTTP ステータス コードも XML 応答もありません ... nada.

タイムアウトの設定が低すぎることが原因でしょうか? 私は10秒に設定されています

私のコードは次のようになります。

public function putObject($objectPath, $bucket, $accessToken, $metaHeaders)
{
    $version_header =   "x-goog-api-version: 2";
    $project_header =   "x-goog-project-id: ".$this->projectID;
    $timestamp      =   date("r");

    $url = 'https://'.$bucket.'.commondatastorage.googleapis.com/object';

    $fp = fopen($objectPath, 'r'); 

    $headers    = array('Host: '.$bucket.'.commondatastorage.googleapis.com',
                    'Date: '.$timestamp, $version_header, 'Content-Type: text/plain',
                    $project_header, 'Content-Length: '.filesize($objectPath),
                    'Authorization: OAuth '.$accessToken);
    if(isset($metaHeaders))
    {
        foreach($metaHeaders as $metaHeader)
        {
            array_push($headers,$metaHeader);
        }
    }

    $c   = curl_init();
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_PUT, 1);
    curl_setopt($c, CURLOPT_INFILE, $fp); 
    curl_setopt($c, CURLOPT_INFILESIZE, filesize($objectPath)); 
    curl_setopt($c, CURLOPT_HEADER, 1);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($c, CURLOPT_TIMEOUT, 10); //timeout in 10s
    curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($c);
    curl_close($c);
    fclose($fp);

    // split up the response into header and xml body
    list($header, $xml) = explode("\r\n\r\n", $response, 2);
    // tokenize 
    $status = strtok($header, "\r\n"); 

    if(stristr($status,"200 OK"))
    {
        //success
        $result = "success";
        //check xml object for specific bucket creation errors

    }
    else
    {
        //failed
        $result = "fail";
        //check xml object for specific bucket creation errors

    }

    return $result;
}
4

1 に答える 1

1

リクエストのサイズによっては、10 秒では不十分な場合があります。タイムアウトを 60 秒から開始し、リクエストに時間がかかる場合はそれを増やします。そして、これがインターネットです。一部のリクエストはタイムアウトになり、再試行する必要がありますが、これは 0.001% よりはるかに短い時間です。

于 2012-10-02T13:19:43.200 に答える