how to read an http get request from a php server
I have a client in php who make开发者_如何转开发 an http get request to a server. that's the code:
client
<?php
function xml_post($xml_request)
{
$url="http://localhost/malakies/server.php?xml=" . urlencode($xml_request);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl_exec($ch);
if (curl_errno($ch)){
$ERR .= "cURL ERROR: ".curl_errno($ch).": ".curl_error($ch)."\n";
}
return $result;
}
$result=xml_post("Send sth");
echo $result; ?>
and the server code:
<?php
$postdata = $_GET['xml'];
echo $postdata; ?>
All work perfect. But i have a question that it may be a rookie one:) I want in the server side to have sth like a listener that listens when an http get request have come and do sth with this request. i don't know if http request is the technique that gives me an option like this.. i want sth like that:
while(http request hasn't come yet)
just wait;
do sth with the http request.
Thank you in advance.
PHP script is ran automatically for each separate request. So actually PHP/Apache is already doing what you're asking for.
Maybe this is a bit confusing if you're coming from different programming language (like Java) where you typically have an event loop waiting for new connection.
On the other hand, maybe you had a specific situation in your mind. Please explain your requirements further if that's the case ...
Because your url "ends" in server.php, you need to place a file on your server named "server.php". If your curl script returns a 404 error, you don't have the file at the right location. Where you need to place the file, depends on the operating system. On Linux, this COULD be /var/www/. So you need to find out what your "document root" is. There you would create a subdir malakies. In the Linux example this would be /var/www/malakies/server.php. PHP will then execute the script inside your file when the request comes in. The data you pass will be placed in an associative array named $_GET. I suggest the following contents for server.php:
<?php
echo "Have a first line so you see something even when no data is passed\n";
var_dump($_GET['xml']);
?>
xml_post in you curl function will then return (disregard the colors)
Have a first line so you see something even when no data is passed
Send sth
If it's not working, what's the error code you get?
I assumed that you have apache installed and that you want to catch the request with PHP.
精彩评论