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.
PHP and https page source
Moderator: General Moderators
Re: PHP and https page source
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
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
Here is a basic example http://us2.php.net/manual/en/function.curl-init.php
And the options are here: http://us2.php.net/manual/en/function.curl-setopt.php
And the options are here: http://us2.php.net/manual/en/function.curl-setopt.php
Re: PHP and https page source
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)
<?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
You may not have curl installed.
I just tested this and it worked ok..
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
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.
Thanks for your help
- 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)
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);
?>