2017-03-25 60 views
0

我需要使用php來發送大字符串(6000個字符)到服務器,該字符串是動態創建的PHP。php CURL帖子大字符串

$ch = curl_init('http://localhost:8080/mypost');                  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json',                     
    'Content-Length: ' . strlen($data_string))                  
);  
$result = curl_exec($ch); 

它工作正常時,data_string很小,但一旦它得到超過幾千字符它掛起。在php控制檯沒有錯誤,只是沒有完成。有趣的是,它在Quercus PHP(不使用libCurl但是它自己的實現)中工作正常,但它在標準php(PHP 5.3.10-1ubuntu3.26)中不起作用。我需要讓它在標準的php中工作。

接受服務器是一個JVM-Grizzly服務器。我在Java中編寫了另一個客戶端,並且它可以很好地發佈字符串,所以我認爲問題不在服務器中。

我看到這篇文章(https://forums.phpfreaks.com/topic/143602-long-string-passed-to-curl-postfields-not-getting-posted/)這似乎很有前途,但無法獲得解決方案的工作。

+0

如果指定'CURLOPT_POSTFIELDS',POST'默認情況下,假定'。刪除'curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,「POST」);'它是否工作? –

+0

不,它沒有炒鍋,仍然在Quercus PHP中工作,雖然 – adamM

+0

如果可以的話,發佈真實的URL,以便我們可以測試它。你可以隨時更換它。 –

回答

0

使用選項來增加默認的緩衝區大小CURLOPT_BUFFERSIZE

試試這個:

$ch = curl_init('http://localhost:8080/mypost');                  
curl_setopt($ch, CURLOPT_BUFFERSIZE, 84000); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json',                     
    'Content-Length: ' . strlen($data_string))                  
);  
$result = curl_exec($ch);