stream_client.php 611 B

123456789101112131415161718192021222324
  1. <?php
  2. $contextOptions = [
  3. 'ssl' => [
  4. 'verify_peer' => false,
  5. // 'allow_self_signed' => true,
  6. // 'cafile' => __DIR__.'/privkey.pem',
  7. 'peer_name' => 'example.com',
  8. ]
  9. ];
  10. $context = stream_context_create($contextOptions);
  11. $fp = stream_socket_client("ssl://127.0.0.1:9501", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
  12. if (!$fp)
  13. {
  14. die("Unable to connect: $errstr ($errno)");
  15. }
  16. stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
  17. $ret = fwrite($fp, "hello\n");
  18. var_dump($ret);
  19. $recv = fread($fp, 8192);
  20. var_dump($recv);
  21. echo "finish\n";