Jump to content

PHP slowness with POST request


FKereki

Recommended Posts

I'm trying to access a web service that requires a POST operation, and I've managed to so in at least three different ways, but it's always SLOW!!

 

My first try used fsockopen() and associated functions, and took about 30 seconds to get an answer. Then I tried the socket_xxxxx() functions, but the results were similar. Finally I used the curl_xxxxx() functions, and yet the same result.

 

HOWEVER, I also did a similar try with Python, and I got the answer within 2 seconds!

 

All the PHP programs worked fine, and so did the Python one, but I couldn't get any speed out of PHP.

 

Are there any known restrictions/problems with this? Any solutions?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/47841-php-slowness-with-post-request/
Share on other sites

I personally have had no problems running a POST script from PHP using curl or sockets.  I have managed to retrieve other pages in a matter of 2-4 seconds.  The only thing there is to blame right now is possibly poor code...don't get me wrong, I'm not trying to attack yoru coding skills, but I'm just pointing out that that's all that is coming to mind right now.

This is some of the actual code; three different ways of reading the stream are commented, and they all work -- though equally slowly:

 

$socket= fsockopen($ipToOpen, $portToOpen) or die("could not open");

fwrite($socket, "POST $pathToOpen HTTP/1.1".$newline);

fwrite($socket, "Host: $urlToOpen".$newline);

fwrite($socket, "Accept-Encoding: identity".$newline);

fwrite($socket, "Connection: close".$newline);

fwrite($socket, "Content-Type: binary/octet-stream".$newline);

fwrite($socket, "Content-Length: ".strlen($dataToSend).$newline);

fwrite($socket, $newline);

fwrite($socket, $dataToSend);

fwrite($socket, $newline);

 

$answer= "";

/* works, but slowly

$answer= stream_get_contents($socket);

*/

 

/* works, but slowly

while (!feof($socket)){

        $answer.= fread($socket,512);

}

*/

 

/* also slow...

$answer= fread($socket);

*/

fclose($socket);

 

 

The (equally slow) curl way:

 

$ch = curl_init();    // initialize curl handle

curl_setopt($ch, CURLOPT_URL, $urlToOpen.$pathToOpen); // set url to post to

curl_setopt($ch, CURLOPT_POST, 1); // set POST method

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable

curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);

curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Accept-Encoding: identity"));

curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Connection: close"));

curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: binary/octet-stream"));

curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Length: ".strlen($dataToSend)));

curl_setopt($ch, CURLOPT_POSTFIELDS, $dataToSend);

$answer = curl_exec($ch); // run the whole process

curl_close($ch);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.