2012-02-28 36 views
0

谷歌顯示411錯誤,但我已經把內容長度的頭。我如何解決這個錯誤?411錯誤,即使內容長度是有

$authToken = getAuthorizationToken(); 
$xml_data = '<?XML version="1.0"?> 
    <Batch> 
    <Remove>  
    <Promotions>  
    <Promotion id="d5111e0a"/> 
    </Promotions> 
    </Remove> 
</Batch>'; 
$length = strlen($xml_data); 
$ch = curl_init("http://www.google.com/cse/api/default/promotions/pe0dnd27zuc"); 
$header = array(); 
$header[] = 'Authorization: GoogleLogin auth=' . $authToken; 
$header[] = 'Content-Type: text/xml'; 
$header[] = 'Content-Length: ' . $length; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); 
$result = curl_exec($ch); 
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 
+0

什麼想法?當我刪除$頭[] = '的Content-Length:'。 $長度;和curl_setopt($ CH,CURLOPT_POSTFIELDS,$ xml_data); - 代碼正常工作並向我發送所有促銷ID xml – propostaff 2012-02-28 04:27:59

回答

2

取出的Content-Length部分從你的頭array.cURL會自動添加它。所以你不需要發送。刪除:

 

//remove following 
$header[] = 'Content-Length: ' . $length; 
 

希望它可以幫助

+0

HTTP 1.1錯誤411引用長度要求:服務器拒絕接受沒有定義的Content-Length的請求。但我認爲這是通過捲曲動作本身添加的。 – Raptor 2012-02-28 04:20:30

+0

它並沒有幫助。仍然是411錯誤.. – propostaff 2012-02-28 04:20:49

0

試試這個:

curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
    curl_setopt($ch,CURLOPT_POST, $length); //count of your posted array 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); 
    $result = curl_exec($ch); 
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 

你缺少發佈字段的長度/計數。

相關問題