简体   繁体   中英

php CURL post large string

I need to use php to post a large string (6000 characters) to a server, the string is dynamically created in the php.

$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);

It works fine when the data_string is small, but once it gets to be over a few thousand chars it hangs. No error in php console, just does not finish. Interestingly, it works fine in Quercus PHP (which does not use libCurl but its own implementation), but it does not work in standard php (PHP 5.3.10-1ubuntu3.26). I need to have it work in standard php.

The accepting server is a JVM-Grizzly server. I wrote another client in Java and it works fine posting the strings, so I don't think the problem is in the server.

I saw this post ( https://forums.phpfreaks.com/topic/143602-long-string-passed-to-curl-postfields-not-getting-posted/ ) which seems promising, but could not get a solution to work.

Increase the default buffer size using the option CURLOPT_BUFFERSIZE

Try this:

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM