Domanda

Non sto ottenendo alcun tipo di risposta da Google.Direi che la richiesta riesce il 99% delle volte, ma ho recentemente iniziato a notare che la richiesta non ha successo.Non fallisce perché non vi è alcuna risposta restituita per indicare un errore: nessun codice di stato HTTP, nessuna risposta XML ... nada.

Potrebbe essere causato impostando il timeout troppo basso?Il mio è impostato a 10s

Ecco come appare il mio codice:

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;
}
.

È stato utile?

Soluzione

10 secondi potrebbe non essere sufficiente a seconda delle dimensioni della richiesta.Inizio il mio timeout a 60 secondi, aumentalo se le richieste richiedono molto tempo.E, questo è Internet.Alcune richieste dovranno timeout e devono essere ritentate, ma questo dovrebbe essere molto meno che lo 0,001% del tempo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top