How to check if a connection to a REST API is secure?
1) I’m building an API and want to make sure that the connection is running SSL before the response is generated and returned to the client.. Username and password is passed in every request
2) And how can you make a fsockopen connection with SSL?
$this->url = 'api.domain.com';
$this->path = '/v1/json/account/user/pass/?id=123';
if($this->fp = fsockopen($this->url, $this->port, $errno, $errstr, $this->timeout)){
$write = "$this->method $this->path HTTP/1.1\r\n";
$write .= "Host: $this->url\r\n";
$write .= "Content-Type: $this->content_type\r\n";
$write .= "Connection: Close\r\n\r\n";
fwrite($this->fp, $write);
while($line = fgets($this->fp)){
if($line !== false) $this->response .= $line;
}
fclose($this->fp);
}
Just check
$_SERVER['HTTPS'].The PHP manual at http://php.net/manual/en/reserved.variables.server.php says: