Reputation: 553
Server A is sending messages to Server B. I need to insert Server C with PHP script between them.
So I want it to work like this:
Server A -> Server C (do some stuff with data and forward request further) -> Server B.
I need Server B to recieve exactly the same request, that was sent from server A. I don't need Server C acting as proxy, just send request as is, and that's all
How could I do it?
I've tried something like this, but in did not work:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'Server B URL');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
$response = curl_exec($ch);
Server B returned 200, but did nothing. I don't have access to it, so I don't know what blocked the request.
Upvotes: 4
Views: 9141
Reputation: 21493
making sure curl exactly replicates any request to the smallest detail? that's not really a job for curl, but for socket_*. i bet some would say that this is not really a job for PHP either, but PHP surely is capable of doing it, in a single-threaded manner. use the socket api to accept connections from server A, read the request, do your processing, forward the request to server B as it was received, read the response from server B, and send that response back to server A.
example (could call this ServerC.php):
<?php
declare(strict_types = 1);
if(php_sapi_name() !== 'cli'){die('this script must run in cli mode.');}
$port = 9999;
$targetIP = gethostbyname ( 'example.org' );
$targetPort = 80;
assert ( false !== filter_var ( $targetIP, FILTER_VALIDATE_IP ) );
var_dump ( $targetIP, $targetPort );
y ( ($listen = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP )) );
register_shutdown_function ( function () use (&$listen) {
socket_close ( $listen );
} );
y ( socket_bind ( $listen, '0.0.0.0', $port ) );
y ( socket_listen ( $listen ) );
y ( socket_set_block ( $listen ) );
echo 'listening for connections... ', PHP_EOL;
while ( false !== ($newconn = socket_accept ( $listen )) ) {
echo 'new connection from ';
socket_getpeername ( $newconn, $peername, $peerport );
echo $peername . ':' . $peerport . PHP_EOL;
y ( socket_set_block ( $newconn ) );
$data = '';
echo 'reading request... ';
$data = socket_read_all ( $newconn, 2 );
var_dump ( $data );
echo 'done.' . PHP_EOL;
// client didnt send any bytes in a while, assume we got the whole request...
{
// do whatever processing you need to do between requests in here.
}
{
// now to forward the request.
y ( ($targetSock = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP )) );
y ( socket_set_block ( $targetSock ) );
y ( socket_connect ( $targetSock, $targetIP, $targetPort ) );
echo 'connected to target. forwarding request... ';
socket_write_all ( $targetSock, $data );
echo 'done.', PHP_EOL;
unset ( $data, $newdata );
$data = '';
echo 'reading response... ';
$data = socket_read_all ( $targetSock, 2 );
var_dump ( $data );
echo 'done.', PHP_EOL;
socket_close ( $targetSock );
}
echo 'sending response back to client... ';
socket_write_all ( $newconn, $data );
echo 'done.', PHP_EOL;
socket_close ( $newconn );
}
function y($in) {
if (! $in) {
$str = hhb_return_var_dump ( socket_last_error (), socket_strerror ( socket_last_error () ) );
throw new \Exception ( $str );
}
return $in;
}
function n($in) {
if (! ! $in) {
throw new \Exception ();
}
return $in;
}
function hhb_return_var_dump(): string // works like var_dump, but returns a string instead of printing it.
{
$args = func_get_args ();
ob_start ();
call_user_func_array ( 'var_dump', $args );
return ob_get_clean ();
}
function socket_write_all($sock, string $data) {
$len = strlen ( $data );
while ( $len > 0 ) {
$written = socket_write ( $sock, $data );
if ($written === false) {
throw new RuntimeException ( 'socket_write failed. errno: ' . socket_last_error ( $sock ) . '. error: ' . socket_strerror ( socket_last_error ( $sock ) ) );
}
$len -= $written;
$data = substr ( $data, $written );
}
return; // all data written
}
function socket_read_all($sock, int $sleep_sec = 2): string {
$ret = '';
while ( true ) {
sleep ( $sleep_sec ); // ...
$buf = '';
$read = socket_recv ( $sock, $buf, 9999, MSG_DONTWAIT );
if ($read === false || $read < 1) {
return $ret;
}
$ret .= $buf;
}
}
(this script is slow, processes only 1 request at a time (concurrent requests are buffered by the OS for socket_accept, though), and synchronous. but it could be optimized and made fully async with socket_select & co, if its worth optimizing)
Upvotes: 6
Reputation: 1971
Try changing this line
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
to:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
When you make a regular cURL request, you cannot use an array ($_POST
is an array) directly as a value for CURLOPT_POSTFIELDS
. You have to transform that array into a postdata string, which is exactly what http_build_query()
does. It converts for example
["name" => "my first name", "email" => "[email protected]"]
into:
name=my%20first%20name&[email protected]
Upvotes: -2