Php get http connection

how to read an http get request from a php server

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. 

It is a strange request. In a sense, your webserver already does this for you. It triggers your PHP script when the request comes in. Is that insufficient in some way?

You have Apache doing this job (listening to incoming HTTP requests). Or you want to keep the connection with the client opened for long time periods? What is the aim of this?

2 Answers 2

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 .

Ok to explain.. Like java you have for example an event function like click one that is triggered when an element be clicked. In similar way i would like a function like this in php which will be triggered when an http request comes. Assume that client and server are two web sites. if i delete the command «echo $result;» from the client code i want when i open the «server» site to see the string «send sth». if i have two tabs open in a browser (clent and server) when i refresh the client i want to see the string «send sth» in the server side not in the client.

Читайте также:  Sign Up

Well the situation you described matches my answer. PHP module runs requested script for each request seperately. That means if you run the client code twice, server code will also be ran twice (since you’re doing a request from the client twice). If your server code wants to store information about the client (e.g. you want to store the data client has sent), you use sessions. Session is typically identified via cookie that is stored at client (normally browser), but since you’re using CURL, you probably are gonna need to use cookiejar for sessions (php.net/manual/en/book.curl.php)

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:

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.

Источник

PHP how to connect with sockets and read the response?

The title explains it all. How can i connect to an IP using tcp protocol and read/get the response? I have searched a lot but i didnt find any solution.

$socket = stream_socket_server("tcp://127.0.0.1:22", $errno, $errstr); if (!$socket) < echo "$errstr ($errno)
\n"; > else < while ($conn = stream_socket_accept($socket)) < echo fread($conn, 26); fclose($conn); >fclose($socket); >

I am connecting to a server, which listens to a port and then i need to get the response from the server.

You might want to take a look at the documentation for socket_stream_server; you’re creating a server, not connecting to one. Also, port 22 is a special port for SSH, I’d avoid using it if at all possible. php.net/manual/en/function.stream-socket-server.php

2 Answers 2

As suggested by others; avoid using port 22. I recommend using an obscure (unused) socket port number > 1024 such as 4444. Anything below 1024 normally requires root access. If you need to test connectivity for 22 have your server script run additional functions.

As for sending a response back to the connected client use stream_socket_recvfrom($socket, $length, 0, $peer) instead of fread()

Then on the client side add a response listener:

$socket = stream_socket_client('tcp://127.0.0.1:4444'); if ($socket) < $sent = stream_socket_sendto($socket, 'message'); if ($sent >0) < $server_response = fread($socket, 4096); echo $server_response; >> else < echo 'Unable to connect to server'; >stream_socket_shutdown($socket, STREAM_SHUT_RDWR); 
$conn = stream_socket_server('tcp://127.0.0.1:4444'); while ($socket = stream_socket_accept($conn)) < $pkt = stream_socket_recvfrom($socket, 1500, 0, $peer); if (false === empty($pkt)) < stream_socket_sendto($socket, 'Received pkt ' . $pkt, 0, $peer); >fclose($socket); usleep(10000); //100ms delay > stream_socket_shutdown($conn, \STREAM_SHUT_RDWR); 

Run server.php which will listen in an endless loop listening for a non-empty packet once server.php receives a packet it will respond back to the connected client with the received packet.

Then execute client.php which will send ‘message’ to server.php Once sent it will then retrieve and echo the response from server.php which should read ‘Received pkt message’

Источник

Оцените статью