Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

php - 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 HTTP 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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';    
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...