How can I do an SSL connection with PHP

19,798

Solution 1

Found it ! Problem was with the certificate. Since I did not create it myself I wasn't investigating it but I was wrong...

I decided to regenerate it myself properly following these instructions : Error using ssl cert with PHP and it works !

Thanks for your help :)

Solution 2

Not exactly on topic, but worth a mention:

Two things are important in order to make SSL work with PHP (if you are compiling PHP from source)

1) make sure the openssl base and devel packages are installed e.g. yum install openssl openssl-devel

2) compile PHP with openssl support: i.e. use the --with-openssl option when running config.

Then, ssl will appear in the "Registered Stream Socket Transports" in phpinfo()

Share:
19,798
Anth0
Author by

Anth0

Fullstack software craftsman

Updated on June 04, 2022

Comments

  • Anth0
    Anth0 almost 2 years

    I need to develop a PHP class to communicate with Apple servers in order to do Push notification (APNS). I have the certificate (.pem) and I tried to follow various tutorials found on Internet but I'm still getting error trying to connect to ssl://gateway.sandbox.push.apple.com:2195 with stream socket :

    $apnsHost = 'gateway.sandbox.push.apple.com';
    $apnsPort = 2195;
    $apnsCert = 'apns-dev.pem';
    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
    $apns = stream_socket_client('ssl://'.$apnsHost.':'.$apnsPort, $error, $errorString, 2,
    STREAM_CLIENT_CONNECT, $streamContext);
    

    A telnet on this URL works so port 2195 is opened. Openssl is activated on PHP since I get "Registered Stream Socket Transports : tcp, udp, ssl, sslv3, sslv2, tls" with a phpinfo(). My certificate is well read (PHP is_readable(certif.pem) returns true on the file)

    Is there anything else to activate in Apache or PHP to get it work ?