PHP and https page source

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

PHP and https page source

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP and https page source

Post by Benjamin »

You may need to use curl with the CURLOPT_SSL_VERIFYPEER option set to false if the certificate is not being accepted.
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: PHP and https page source

Post by Dynamis »

I am unfamiliar with the curl functions. What would I use to pull the page source once I set that?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP and https page source

Post by Benjamin »

Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: PHP and https page source

Post 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)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP and https page source

Post 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) ;
 
?>
 
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: PHP and https page source

Post 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
Post Reply