2016-02-18 38 views
1

現在差​​不多兩天,試圖將a * .gzip(或* .gz)文件上傳到api。下面的代碼上傳文件,但噴氣阿比說「解析文件時出錯:。神奇的數字在gzip頭信息是不正確確保您傳遞一個gzip流用php curl將a * .gzip文件上傳到api url

function getCurlValue($filename, $contentType, $postname){ 
    // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax 
    // See: https://wiki.php.net/rfc/curl-file-upload 
    if (function_exists('curl_file_create')) { 
     return curl_file_create($filename, $contentType, $postname); 
    } 

    // Use the old style if using an older version of PHP 
    $value = "@{$filename};filename=" . $postname; 
    if ($contentType) { 
     $value .= ';type=' . $contentType; 
    } 

    return $value; 
} 

function curl_upload_test($file, $remote_url){ 

$filename = 'gzip-test.gz'; 

$cfile = getCurlValue($filename, 'application/x-gzip', $filename); 


$data = array('file' => $cfile, 'method' => 'PUT'); 

$ch = curl_init(); 
$options = array( CURLOPT_URL => $remote_url, 
        CURLOPT_RETURNTRANSFER => true, 
        CURLOPT_CUSTOMREQUEST => $data['method'], 
        CURLOPT_HTTPHEADER => array(
              'Accept:', 
              'Expect:', 
              'Content-Type: application/x-gzip', 
              'x-ms-blob-type: blockblob' 
             ), 
        CURLINFO_HEADER_OUT => true, //Request header 
        CURLOPT_HEADER => 0, //Return header 
        CURLOPT_SSL_VERIFYPEER => false, 
        CURLOPT_POST => true, 
        CURLOPT_POSTFIELDS => $data 
      ); 

curl_setopt_array($ch, $options); 
$result = curl_exec($ch); 
$header_info = curl_getinfo($ch,CURLINFO_HEADER_OUT); 
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
$header = substr($result, 0, $header_size); 
$body = substr($result, $header_size); 
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 

return $httpcode; 
} 

我通過wen't他們的文檔,並發現用C#編寫* .gzip文件的jet api示例代碼。

///Url returned by GET /api/files/uploadToken 
    var url = "https://imupload.blob.core.windows.net/merchant-files/b21611714c8d4734a7686e86f7a8080d?sv=2014-02-14&sr=b&sig=05ssdgrshow383JVEmqk%2BDwrTlHqfEO16gApw%2BgK6%2F2k%3D&se=2014-12-02T17%3A13%3A16Z&sp=w"; 

    var request = WebRequest.CreateHttp(url); 
    request.Method = "PUT"; 
    request.Headers.Add("x-ms-blob-type:BlockBlob"); 

    using (var stream = File.OpenRead(@"C:\temp\test.json")) 
    using(var zipped = new GZipStream(request.GetRequestStream(), CompressionMode.Compress)) 
    stream.CopyTo(zipped); 

    request.GetResponse(); 

任何幫助這個高度讚賞。

+0

你完全確定gzip-test.gz確實是一個gzip壓縮文件嗎?從你遇到的錯誤判斷,端點API不這麼認爲。 –

+0

@Sergey是100%確定該文件是* .gzip壓縮文件。因爲當我通過POSTMAN嘗試與同一個文件相同的東西時,所有工作都很好。一旦我使用php應用程序端點不會將其識別爲* .gzip文件。 – Shafraz

回答

0

我想你應該嘗試使用實際的PUT方法而不是POST(請參閱示例here)。另外,我覺得你的$data(你進入CURLOPT_POSTFIELDS)必須包含您的GZIP文件的實際二進制數據:

$data = file_get_contents($filename); 

,而不是一個古怪的陣列。

如果這沒有幫助,那麼將非常感謝您使用的API文檔的鏈接。

+0

非常感謝你的兄弟。它工作,我很放心。 – Shafraz