0

I am writing a script which works similar as a bot and I try to parse out certain data from an url. I'm doing the "main" request with curl and a Dom Element for parsing the webpage, however I didnt find a solution to check if the site has a valid ssl certificate with Curl as well. Instead I'm doing it with fopen like this:

$stream = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$read = fopen($url, "rb", false, $stream);
$cont = stream_context_get_params($read);
$var = ($cont["options"]["ssl"]["peer_certificate"]);
$result = (!is_null($var)) ? true : false;

if($result == true){ 
     $returnData["ssl"] = 1; // index 7
    } else {
     $returnData["ssl"] = 0; // index 7
    }

Now sometimes the fopen won't work correctly and return the following error:

fopen(http://): failed to open stream: operation failed 

So I either want to check if the Site uses a SSL with curl or atleast return an empty string when this error occurs and then continue with the rest of the script because the data needs to be sent back to ajax at the end of the script. If not - Ajax will fail and I get a 504 back, because the script is running for too long.

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
digitalsuite.net
  • 339
  • 6
  • 20
  • Just check if the call to `fopen` was successful. – miken32 Oct 09 '18 at 18:38
  • Well, yes but I would rather use curl to have a reliable solution. – digitalsuite.net Oct 09 '18 at 18:45
  • It won't behave much differently when the site is unavailable. – miken32 Oct 09 '18 at 18:46
  • ok. Perfomance wise speaking, wouldn't it be better to cut it down to one request to the url per script instead of 2 or more? – digitalsuite.net Oct 09 '18 at 18:48
  • 1
    Check this out: https://stackoverflow.com/a/3817143/1255289. You won't get the actual certificate, but the subject, issuer, expiry are all there. – miken32 Oct 09 '18 at 18:51
  • Well thanks for the thread, however I used https://stackoverflow.com/a/3081093/8177490 to save the extra request. But on Domains like example.com which dont have a 301 but ssl it returns ssl even if I check http://example.com, because it checks the port. Is there any workaround? – digitalsuite.net Oct 09 '18 at 19:18

0 Answers0