PHP captures HTTPS content, and php captures https content
I recently encountered an HTTPS problem when studying the Hacker News API. Because all Hacker News APIs are accessed through the encrypted HTTPS protocol, unlike the common HTTP protocol, when using functions in PHPfile_get_contents()
An error occurs when you obtain the data provided by the API. The code used is as follows:
<?php
$data = file_get_contents("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty");
......
When the above Code is run, the following error message is displayed:
PHP Warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?
Below is:
Why is this error?
After some searches on the Internet, I found that there were still many people who encountered such errors. The problem was very direct, because a parameter was not enabled in the PHP configuration file, which is/apache/bin/php.ini
In;extension=php_openssl.dll
Remove the semicolon. You can use the following script to check the configuration of your PHP environment:
$w = stream_get_wrappers();
echo 'openssl: ', extension_loaded ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_dump($w);
Run the above script snippet and the result on my machine is:
openssl: no
http wrapper: yes
https wrapper: no
wrappers: array(10) {
[0]=>
string(3) "php"
[1]=>
string(4) "file"
[2]=>
string(4) "glob"
[3]=>
string(4) "data"
[4]=>
string(4) "http"
[5]=>
string(3) "ftp"
[6]=>
string(3) "zip"
[7]=>
string(13) "compress.zlib"
[8]=>
string(14) "compress.bzip2"
[9]=>
string(4) "phar"
}
Alternative
It is very easy to find and correct errors. However, it is difficult to correct errors after they are found. I originally wanted to run this script on a remote host, but I was unable to modify the PHP configuration of the remote host. As a result, I was not able to use this scheme, but we could not throw it on a tree, there is no way through this road to see if there is any other way.
Another function that I often use to capture content in PHP iscurl
, It is betterfile_get_contents()
More powerful, providing many optional parameters. ForHTTPS
Content problems, we need to useCURL
The configuration parameters are:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
You can see from the semantics that it ignores/skips SSL security authentication. This may not be a good practice, but it is sufficient for common scenarios.
Below is the usageCurl
Encapsulate a function that can access HTTPS content:
function getHTTPS($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
The above is the entire process for php to obtain https content. It is very simple and practical. We recommend it to partners who have the same project requirements.