I need to send request and receive response over a ssl https connection. One requirements is to only use low level socket function (NOT STREAM), such as socket_write/socket_read, socket_send/socket_recv, listed on http://php.net/manual/en/ref.sockets.php
Asked
Active
Viewed 773 times
1
-
you could use some code from [here](http://stackoverflow.com/questions/12277982/php-stream-download-website-content-until-string-found) – Stefan Jan 12 '13 at 12:51
-
@hek2mgl I tried the code on http://php.net/manual/en/function.socket-recv.php, but it was for a http request NOT a ssl request – Mickey Shine Jan 12 '13 at 12:55
-
@Stefan fsockopen is NOT what I want. I need to use socket_create – Mickey Shine Jan 12 '13 at 12:56
-
We won't do your homework btw – Stefan Jan 12 '13 at 12:59
-
@Stefan Oh this is really not my homework. I can use socket functions to handle normal http request. But for ssl connections, the low level socket functions seem not that easy. I tried to use Ev library for async processing, and it can only monitor FD/Sockets so that I must use the low level socket functions – Mickey Shine Jan 12 '13 at 13:08
-
Didn't know about `stream_socket_enable_crypto()`. See my answer below. Good question! :) +1 – hek2mgl Jan 12 '13 at 13:31
1 Answers
0
You can use the method stream_socket_enable_crypto()
on the connected socket. To enable ssl on the socket there are 4 possible options that depend on environment and version. you can try each of them unless one will work. Image this example:
function enableCrypto($socket)
{
$modes = array(
STREAM_CRYPTO_METHOD_TLS_CLIENT,
STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
);
foreach ($modes as $mode) {
if (stream_socket_enable_crypto($socket, true, $mode)) {
return;
}
}
throw new Exception('failed to enable crypto');
}
Btw, the example is taken from the HTTP_Request2 package from the PEAR repositories. The function above is nearly the same as HTTP_Request2_SocketWrapper::enableCrypto(). You may find further information by have a look at the source code of HTTP_Request2.

hek2mgl
- 152,036
- 28
- 249
- 266
-
I tried to pass stream_socket_enable_crypto to $socket created by socket_create, but none of the modes can be set to $socket. This function might be still for stream – Mickey Shine Jan 12 '13 at 13:42
-
Yes, sorry I tried it too. :( . Is using streams an option? (Would suggest) Should I prepare an example? – hek2mgl Jan 12 '13 at 13:44
-
I can use streams if the Ev library supports. But EvIo does not seem to monitor stream. I am still trying to find a solution. Thanks for your reply – Mickey Shine Jan 12 '13 at 13:50
-
Interesting stuff the `Ev` extension. Will dig into it. Can I understand your question like 'How can I perform a https request using the Ev extension?' ? – hek2mgl Jan 12 '13 at 13:56
-