Page 1 of 1

PHP and https page source

Posted: Thu Jul 10, 2008 3:20 pm
by Dynamis
Hello,

I have run into a problem that I was hoping someone could assist me with. I am trying to pull the page source from an https web page and have currently not come across a way of doing it through php. I have tried fsocketopen, fopen, file_get_contents and numerous other ways. All of these seem to work fine for http pages, but I have been unsuccessful trying to do so with an https page. Any help would be appreciated.

Re: PHP and https page source

Posted: Thu Jul 10, 2008 3:24 pm
by Benjamin
You may need to use curl with the CURLOPT_SSL_VERIFYPEER option set to false if the certificate is not being accepted.

Re: PHP and https page source

Posted: Thu Jul 10, 2008 3:31 pm
by Dynamis
I am unfamiliar with the curl functions. What would I use to pull the page source once I set that?

Re: PHP and https page source

Posted: Thu Jul 10, 2008 3:33 pm
by Benjamin

Re: PHP and https page source

Posted: Thu Jul 10, 2008 3:59 pm
by Dynamis
Right now I am still unable to even get it working for a non-https site, such as google. Here is my code:

<?php
$ch = curl_init ('http://www.google.com/') ;
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0) ;
$res = curl_exec ($ch) ;
curl_close ($ch) ;

echo $res;
echo strlen($res);

?>

Also, even w/out the verifypeer option, it still does nothing when this code is run (blank screen)

Re: PHP and https page source

Posted: Thu Jul 10, 2008 4:05 pm
by Benjamin
You may not have curl installed.

I just tested this and it worked ok..

Code: Select all

 
<?php
$ch = curl_init() ;
 
curl_setopt($ch, CURLOPT_URL, "http://www.google.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 
echo $res = curl_exec($ch);
 
curl_close ($ch) ;
 
?>
 

Re: PHP and https page source

Posted: Thu Jul 10, 2008 4:44 pm
by Dynamis
Thank you, I got it to work. For future reference of people trying to figure this out themselves here are a few things to keep in mind.
  • If you used the windows installer for php, it is set up to not install the curl package by default. To see if you have it installed, open php/ext/ and there should be a php_curl.dll file in there. If it is not, then reinstall
  • You need to set CURLOPT_SSL_VERIFYPEER to false for https pages to work (thanks go to "astions" on that)
The code below works to open https pages and display them to screen:

Code: Select all

 
<?php
 
    $ch = curl_init ("https://SITE_HERE") ;
    //$ch = curl_init ('http://www.google.com/') ;
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $res = curl_exec ($ch) ;
    curl_close ($ch) ;
 
    echo $res;
    echo strlen($res);
 
?>
 
Thanks for your help