Как захватить полные данные запроса HTTP (заголовки и тело) с помощью PHP?

У меня проблема с внедрением API, который работает с Java, но не работает с cURL. Мы до сих пор пережили все, и между запросами, которые делает Java, и что мы делаем, должно быть что-то другое.

В PHP мы можем получить данные заголовка, просматривая переменные $_SERVER['HTTP_*'] и мы можем получить тело запроса от file_get_contents('php://input'); Но мы не можем получить точные данные, отправленные от агента пользователя клиенту.

Можно ли получить полный запрос, который отправляет пользовательский агент, с помощью PHP? Включены заголовки и корпус? Если да, то как?

Единственный пример, который я нашел, здесь , но этот получает тело так, как я упоминал, в то время как он получает заголовки, анализируя через $_SERVER , который кажется взломом, поскольку он никогда не был на 100% от того, что действительно было отправлено.

Вся помощь и советы приветствуются!

для заголовков вы можете попробовать apache_request_headers (), а для тела я не знаю другого метода, кроме file_get_contents('php://input');

Старый вопрос, но для тех, кому это нужно в будущем … Лучшим (возможно, единственным) способом было бы полностью контролировать сервер, будучи сервером.

Настройте сервер сокета, прослушивающий порт 80 (если это все, что вам нужно для работы сервера) или любой другой порт, если 80 недоступен.

Таким образом, вы можете полностью аннулировать запрос. Примеры базовых серверов сокетов многочисленны, вот упрощенная версия последней, которую я реализовал, которая будет печатать полный запрос:

 <?php //Read the port number from first parameter on the command line if set $port = (isset($argv[1])) ? intval($argv[1]) : 80; //Just a helper function dlog($string) { echo '[' . date('Ymd H:i:s') . '] ' . $string . "\n"; } //Create socket while (($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { dlog("socket_create() failed: reason: " . socket_strerror(socket_last_error())); sleep(1); } //Reduce blocking if previous connections weren't ended correctly if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) { dlog("socket_set_option() failed: reason: " . socket_strerror(socket_last_error($sock))); exit; } //Bind to port $tries = 0; while (@socket_bind($sock, 0, $port) === false) { dlog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock))); sleep(1); $tries++; if ($tries>30) { dlog("socket_bind() failed 30 times giving up..."); exit; } } //Start listening while (@socket_listen($sock, 5) === false) { dlog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock))); sleep(1); } //Makes it possible to accept several simultaneous connections socket_set_nonblock($sock); //Keeps track of active connections $clients = array(); dlog("server started..."); while(true) { //Accept new connections while (($msgsock = @socket_accept($sock)) !== false) { //Prevent blocking socket_set_nonblock($msgsock); //Get IP - just for logging socket_getpeername($msgsock, $remote_address); //Add new client to array $clients[] = array('sock' => $msgsock, 'timeout' => time()+30, 'ip' => $remote_address); dlog("$remote_address connected, client count: ".count($clients)); } //Loop existing clients and read input foreach($clients as $key => $client) { $rec = ''; $buf = ''; while (true) { //Read 2 kb into buffer $buf = socket_read($clients[$key]['sock'], 2048, PHP_BINARY_READ); //Break if error reading if ($buf === false) break; //Append buffer to input $rec .= $buf; //If no more data is available socket read returns an empty string - break if ($buf === '') break; } if ($rec=='') { //If nothing was received from this client for 30 seconds then end the connection if ($clients[$key]['timeout']<time()) { dlog('No data from ' . $clients[$key]['ip'] . ' for 30 seconds. Ending connection'); //Close socket socket_close($client['sock']); //Clean up clients array unset($clients[$key]); } } else { //If something was received increase the timeout $clients[$key]['timeout']=time()+30; //And.... DO SOMETHING dlog('Raw data received from ' . $clients[$key]['ip'] . "\n------\n" . $rec . "\n------"); } } //Allow the server to do other stuff by sleeping for 50 ms on each iteration usleep(50000); } //We'll never reach here, but some logic should be implemented to correctly end the server foreach($clients as $key => $client) { socket_close($client['sock']); } @socket_close($sock); exit; не <?php //Read the port number from first parameter on the command line if set $port = (isset($argv[1])) ? intval($argv[1]) : 80; //Just a helper function dlog($string) { echo '[' . date('Ymd H:i:s') . '] ' . $string . "\n"; } //Create socket while (($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { dlog("socket_create() failed: reason: " . socket_strerror(socket_last_error())); sleep(1); } //Reduce blocking if previous connections weren't ended correctly if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) { dlog("socket_set_option() failed: reason: " . socket_strerror(socket_last_error($sock))); exit; } //Bind to port $tries = 0; while (@socket_bind($sock, 0, $port) === false) { dlog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock))); sleep(1); $tries++; if ($tries>30) { dlog("socket_bind() failed 30 times giving up..."); exit; } } //Start listening while (@socket_listen($sock, 5) === false) { dlog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock))); sleep(1); } //Makes it possible to accept several simultaneous connections socket_set_nonblock($sock); //Keeps track of active connections $clients = array(); dlog("server started..."); while(true) { //Accept new connections while (($msgsock = @socket_accept($sock)) !== false) { //Prevent blocking socket_set_nonblock($msgsock); //Get IP - just for logging socket_getpeername($msgsock, $remote_address); //Add new client to array $clients[] = array('sock' => $msgsock, 'timeout' => time()+30, 'ip' => $remote_address); dlog("$remote_address connected, client count: ".count($clients)); } //Loop existing clients and read input foreach($clients as $key => $client) { $rec = ''; $buf = ''; while (true) { //Read 2 kb into buffer $buf = socket_read($clients[$key]['sock'], 2048, PHP_BINARY_READ); //Break if error reading if ($buf === false) break; //Append buffer to input $rec .= $buf; //If no more data is available socket read returns an empty string - break if ($buf === '') break; } if ($rec=='') { //If nothing was received from this client for 30 seconds then end the connection if ($clients[$key]['timeout']<time()) { dlog('No data from ' . $clients[$key]['ip'] . ' for 30 seconds. Ending connection'); //Close socket socket_close($client['sock']); //Clean up clients array unset($clients[$key]); } } else { //If something was received increase the timeout $clients[$key]['timeout']=time()+30; //And.... DO SOMETHING dlog('Raw data received from ' . $clients[$key]['ip'] . "\n------\n" . $rec . "\n------"); } } //Allow the server to do other stuff by sleeping for 50 ms on each iteration usleep(50000); } //We'll never reach here, but some logic should be implemented to correctly end the server foreach($clients as $key => $client) { socket_close($client['sock']); } @socket_close($sock); exit; - <?php //Read the port number from first parameter on the command line if set $port = (isset($argv[1])) ? intval($argv[1]) : 80; //Just a helper function dlog($string) { echo '[' . date('Ymd H:i:s') . '] ' . $string . "\n"; } //Create socket while (($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { dlog("socket_create() failed: reason: " . socket_strerror(socket_last_error())); sleep(1); } //Reduce blocking if previous connections weren't ended correctly if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) { dlog("socket_set_option() failed: reason: " . socket_strerror(socket_last_error($sock))); exit; } //Bind to port $tries = 0; while (@socket_bind($sock, 0, $port) === false) { dlog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock))); sleep(1); $tries++; if ($tries>30) { dlog("socket_bind() failed 30 times giving up..."); exit; } } //Start listening while (@socket_listen($sock, 5) === false) { dlog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock))); sleep(1); } //Makes it possible to accept several simultaneous connections socket_set_nonblock($sock); //Keeps track of active connections $clients = array(); dlog("server started..."); while(true) { //Accept new connections while (($msgsock = @socket_accept($sock)) !== false) { //Prevent blocking socket_set_nonblock($msgsock); //Get IP - just for logging socket_getpeername($msgsock, $remote_address); //Add new client to array $clients[] = array('sock' => $msgsock, 'timeout' => time()+30, 'ip' => $remote_address); dlog("$remote_address connected, client count: ".count($clients)); } //Loop existing clients and read input foreach($clients as $key => $client) { $rec = ''; $buf = ''; while (true) { //Read 2 kb into buffer $buf = socket_read($clients[$key]['sock'], 2048, PHP_BINARY_READ); //Break if error reading if ($buf === false) break; //Append buffer to input $rec .= $buf; //If no more data is available socket read returns an empty string - break if ($buf === '') break; } if ($rec=='') { //If nothing was received from this client for 30 seconds then end the connection if ($clients[$key]['timeout']<time()) { dlog('No data from ' . $clients[$key]['ip'] . ' for 30 seconds. Ending connection'); //Close socket socket_close($client['sock']); //Clean up clients array unset($clients[$key]); } } else { //If something was received increase the timeout $clients[$key]['timeout']=time()+30; //And.... DO SOMETHING dlog('Raw data received from ' . $clients[$key]['ip'] . "\n------\n" . $rec . "\n------"); } } //Allow the server to do other stuff by sleeping for 50 ms on each iteration usleep(50000); } //We'll never reach here, but some logic should be implemented to correctly end the server foreach($clients as $key => $client) { socket_close($client['sock']); } @socket_close($sock); exit; 

Для запуска сервера на порт 8080 просто запустите php filename.php 8080 из оболочки.

Это не «с php», но вы можете найти их полезными для своих целей, тем не менее