开发者

sending a non-blocking HTTP POST request

I have a two websites in php and python. When a user sends a request to the server I need php/python to send an HTTP POST request to a remote server. I want to reply to the user immediately without waiting for a response from the remote server.

Is it possible to continue running a php/python script after sending a response to the user. In that case I'll first reply to the user and only then send the HTT开发者_如何学JAVAP POST request to the remote server.

Is it possible to create a non-blocking HTTP client in php/python without handling the response at all?

A solution that will have the same logic in php and python is preferable for me.

Thanks


In PHP you can close the connection by sending this request (this is HTTP related and works also in python, although I don't know the proper syntax to use):

// Send the response to the client
header('Connection: Close');
// Do the background job: just don't output anything!

Addendum: I forgot to mention you probably have to set the "Context-Length". Also, check out this comment for tips and a real test case.

Example:

<?php    
ob_end_clean();
header('Connection: close');

ob_start();

echo 'Your stuff goes here...';

header('Content-Length: ' . ob_get_length());

ob_end_flush();
flush();

// Now we are in background mode
sleep(10);
echo 'This text should not be visible';    
?>


You can spawn another process to handle the POST to the other server. In PHP you would spawn the process and "disconnect" so you don't wait for the response.

exec("nohup /path/to/script/post_content.php > /dev/null 2>&1 &");

You can then you curl to perform the post. If you want to pass parameters to the PHP script, you can use the getopt() function to read them. Not sure if you would do something similar in Python.


What you need to do is have the PHP script execute another script that does the server call and then sends the user the request.


You have to set a middle man. So in your own server you would have:

  • A web form;
  • A submit handler ( php or python script that handles the form submission );
    • Your handler creates a new file and fill it up with the submission data. You can, for instance, format the data as JSON;
    • So your handler has a single job, save the submitted data in a file and respond the user, nothing else. This should be fast.
  • Create a filesystem event driven cron ( not a time driven cron ). See this and this questions (for a windows and an ubuntu servers respectively).
    • Set your cron to execute a php or python script which will then re-post the data to a remote server.


You have to use fsockopen. And don't listen to the result

<?php

$fp = fsockopen('example.com', 80);

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");

fwrite($fp, $content);


Hookah is designed to solve your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜