Php connect to https server

I’m a coder. Welcome to my blog. Here are some of the records on my job.

Home

Categories

PHP Application — Quick Connect to HTTPS Server

The problem is, what I want to develop an application (probably PHP?), capable of making quick connection to a SSL secured server and send/get data from it. My aim is to make it send the query and read the result as quick as possible. I am trying various solutions and benchmarking them all. I have tried fsockopen() and CURL, however, wondering if there are any solutions how I could improve them?

Читайте также:  Php sql select fetch

Results are as follows: fsockopen():

Questions are: 1) When the query is started to execute on the server, to which I connect — after headers are sent or after I get the page and connections closes? 2) Which is the exect time of CURL to which I should compare the fsockopen() result? Is starttransfer_time even before the headers are sent to server? This would mean that fsockopen() is faster, isn’t it? 3) Any solutions how to tweak SSL connectivity on the server, on which the code is executed? Any tweaks possible to OpenSSL module of PHP (any possible downgrades of crypto?) to make it faster? 4) Any solution to go without SSL to SSL secured server? 5) Would C++ or other solution be any quicker in this case?

Any ideas are welcome 🙂 I got obsessed by this «ms» race 🙂

There is no close-form solution to your question.

About C++, it’s true that PHP is interpreted and C++ is compiled+optimized, but all crypto libraries in PHP are in plain C, so compiled+optimized.

Making a shorter server key fastens the handshake phase a little, according to the server hardware. Also, most important, all network protocols that rely on TCP are affected by network delays when processing transactions, which are different in behaviour from data transfers (like FTP) because of the fact that they are synchronized.

1.You have to close the connection manually

4.Unless you want to write the full SSL protocol by yourself, better use the most performant library available, openSSL

5.Yes and no, at least you may get a performance increase but maybe not significant. In general, I would recommend it

PHP application Localhost connect to the server database

I want to ask about how to connect my localhost application (C:xampp/htdocs/myproject) to database at my server host (www.someweb.somedomain)? Am I possible to do that? If it is, how to connect it at my php config? Right now my config (server.php) is

Is there a way to handle an error when establishing an application-level connection to SQL Server?

I’m talking about this error message that always appears everytime i lose my internet connection or everytime the server becomes offline: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server

PHP, unable to connect to MySQL server

What might be the reason of the following problem. PHP function mysql_connect() works fine if I execute it from command line (php -r «mysql_connect(‘127.0.0.1’, ‘db_user’, ‘db_pass’);»). The same call with the same parameters fails for PHP runni

OOB application — no connection to the server

I have a working Silverlight 4 application, also working in OOB mode; Here are two scenarios: When I Run Web version together with OOB version OOB is working properly, server methods as well Run OOB application only OOB is starting, but server method

Good workflow for the application that connects to the server?

So here is the situation. I have made an app that needs a server to work properly. For making new releases I spin up an test server with the same code, but with a different ip address. Currently I change the ip address in the app itself test the app

Problem with regional characters (polish) in php application and ms sql server

I have a problem with regional string characters inserted to MS SQL Server database. There is a PHP application that connects with mssql server and inserts some data. But instead of inserting characters such as: ą, Ą, ć, Ł, ź (and so on — btw. these

Adobe AIR application as an HTTP server?

Is there any way to use an Adobe AIR application as a local http server? i.e. Adobe AIR application listening to http://localhost:8020 and I’m able to use a browser to access the application? If I can, is it true or pseudo multithreading? Is there an

Exact network time from the php application server to the mysql database server

I got two servers One is application server on which PHP application is running and another server on which Mysql database is running. I want to measure time taken for the request send from application server to request received on database server fo

The Swift iOS application does not connect to the server using Socket.IO

I’m about to write a very simple iOS application. I want the application to connect to a server by using Socket.IO. I’ve already installed Socket.IO with Cocoapods for my project and everything went well. The problem is after I run my server and then

Can a Java application running on a Windows server connect to SQL Server through Windows Authentication

Let me give some background before I ask my question. I’m at a shop that primarily runs Windows. We have several batch applications running on Windows Servers (mostly 2003). Most of these batch applications are written in C# and C++; however we have

Exception of SSL workforce when connecting via https using a self-signed certificate in Android Nougat

In my android application i connect over https. I am using a self signed certificate to connect. It is working on devices below api level 24 (before android nougat).But on android Nougat it throws the SSL Handshake exception : javax.net.ssl.SSLHandsh

Store passwords for MYSQL in the PHP application in a secure way

I have a php application that connects to the database from the code. I am wandering what would be some secure ways of storing these credentials? This is a linux server. EDIT: I just want to hear some informed opinions to get an informed opinion mysl

How to send an e-mail from the php application to a local e-mail

I have a local network in the company with a mail server, how can i make php application built locally on the server to send and email to a local email address? thanks in advanceJust send it with the mail function. It’s up to the system administrator

Detect whether the request was started as an HTTP server or not (rake task, rconsole, etc.)

I’m using EventMachine and Monetarily to start e TCP server along with my rails application. This is started from config/initializers/momentarily.rb. My problem is that it starts also when I run rake tasks, like db:migrate. I only want it to start wh

Источник

How to Use SSL Sockets with PHP

SSL sockets are perfect for sending secure data. With certificates, you can verify the identify of the host, the client, or both. Signed certificates cost money but you can create and self-sign a certificate. Check out the code samples below to see how to generate SSL certificates and create SSL clients and servers. Examples include raw socket communication as well as the common HTTPS protocol.

Generating an OpenSSL PEM

$certificateData = array( 
"countryName" => "US",
"stateOrProvinceName" => "Texas",
"localityName" => "Houston",
"organizationName" => "DevDungeon.com",
"organizationalUnitName" => "Development",
"commonName" => "DevDungeon",
"emailAddress" => "nanodano@devdungeon.com"
);

// Generate certificate
$privateKey = openssl_pkey_new();
$certificate = openssl_csr_new($certificateData, $privateKey);
$certificate = openssl_csr_sign($certificate, null, $privateKey, 365);

// Generate PEM file
$pem_passphrase = 'abracadabra'; // empty for no passphrase
$pem = array();
openssl_x509_export($certificate, $pem[0]);
openssl_pkey_export($privateKey, $pem[1], $pem_passphrase);
$pem = implode($pem);

// Save PEM file
$pemfile = './server.pem';
file_put_contents($pemfile, $pem);

SSL Client Socket

$socket = stream_socket_client("ssl://192.168.1.2:5522", $errno, $errstr);
if ($socket) echo fread($socket, 2000);
>
$host = '192.168.1.2';
$port = 5522;
$timeout = 30;
$cert = 'e:\www\workspace\php\sockets\server.pem'; // Path to certificate
$context = stream_context_create(
array('ssl'=>array('local_cert'=> $cert))
);
if ($socket = stream_socket_client(
'ssl://'.$host.':'.$port,
$errno,
$errstr,
30,
STREAM_CLIENT_CONNECT,
$context)
) fwrite($socket, "\n");
echo fread($socket,8192);
fclose($socket);
> else echo "ERROR: $errno - $errstr\n";
>

SSL Server Socket

$context = stream_context_create();

// local_cert must be in PEM format
stream_context_set_option($context, 'ssl', 'local_cert', $pemfile);

// Pass Phrase (password) of private key
stream_context_set_option($context, 'ssl', 'passphrase', $pem_passphrase);
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);

// Create the server socket
$socket = stream_socket_server(
'ssl://0.0.0.0:9001',
$errno,
$errstr,
STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,
$context
);

// fwrite/fread to $socket

HTTPS Client

// Get port and IP address
$service_port = getservbyname('www', 'tcp');
$address = gethostbyname('www.google.com');

// Bind socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
>

// Connect
$result = socket_connect($socket, $address, $service_port);
if ($result === false) echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
>

// Create HTTP request
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.google.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';

// Send HTTP request
socket_write($socket, $in, strlen($in));

// Read and output response
while ($out = socket_read($socket, 2048)) echo $out;
>

// Close socket
socket_close($socket);

HTTPS Server

This is just a slight extension of the SSL Server code above to accept and respond as an HTTP server

$context = stream_context_create();

// local_cert must be in PEM format
stream_context_set_option($context, 'ssl', 'local_cert', '/path/to/pem/file');
stream_context_set_option($context, 'ssl', 'passphrase', $pem_passphrase);
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);

// Create the server socket
$server = stream_socket_server('ssl://192.168.1.96:9001', $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);

while(true)
$buffer = '';
$client = stream_socket_accept($server);
if($client) // Read until double CRLF
while( !preg_match('/\r?\n\r?\n/', $buffer) )
$buffer .= fread($client, 2046);
// Respond to client
fwrite($client, "200 OK HTTP/1.1\r\n"
. "Connection: close\r\n"
. "Content-Type: text/html\r\n"
. "\r\n"
. "Hello World! " . microtime(true)
. "\n
"); 
fclose($client);
>
>

Источник

Оцените статью