Reputation: 51
I am not sure how can I configure stream context parameter for tcp proxy for streaming socket in php. I found and tested following code but it's not working for stream sockect.
$context = stream_context_create(
array(
'http'=>array(
'proxy'=> 'tcp://'.$proxy,
)
)
);
$srvHandle = stream_socket_client("tcp://{$this->server}", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if ($srvHandle === false)
$this->LogError("failed to connect with host website, check your network connection.");
stream_set_blocking($srvHandle, true);
stream_socket_enable_crypto($srvHandle, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
stream_set_blocking($srvHandle, false);
But the context is working for file_get_contents function.
$context = stream_context_create(
array(
/*
'socket' => array(
'bindto' => $proxy,
)
*/
'http'=>array(
'proxy'=> 'tcp://'.$proxy,
"request_fulluri" => TRUE,
),
"ssl" => array(
"SNI_enabled" => FALSE,
)
)
);
$result = file_get_contents("http://api.ipify.org?format=json", false, $context);
So I can know the context only working for http protocol.
How can I configure the context parameter array for tcp stream socket?
Upvotes: 2
Views: 2681
Reputation: 2307
You can use streams, as per your example you need a client stream socket.
Look at stream_socket_client to create a tcp socket. Note the last parameter, $context
, that should be created using stream_context_create.
There are examples enough in manuals, but here it is on how to use it
$context = stream_context_create(
array(
/*
'socket' => array(
'bindto' => $proxy,
)
*/
'http'=>array(
'proxy'=> 'tcp://'.$proxy,
"request_fulluri" => TRUE,
),
"ssl" => array(
"SNI_enabled" => FALSE,
)
)
);
$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30, ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT, $context);
Also note that fopen also accepts the context, and works with tcp streams too. The stream extension functions provide more options, but for basic use fopen
should be enough.
Upvotes: 1