Recently encountered an HTTPS problem while studying the hacker News API. Because all the hacker News APIs are encrypted by HTTPS
Protocol access, unlike the normal HTTP protocol, when using PHP's function file_get_contents () to get the number provided in the API
In the case of an error, the code used is this:
$data = file_get_contents ("Https://hacker-news.firebaseio.com/v0/topstories.json?")
Print=pretty ");
......
When you run the above code, you encounter the following error message:
PHP warning:file_get_contents (): Unable to find the wrapper "https"-do you forget to
Enable it when do you configured PHP?
Why is there such a mistake?
After a search on the internet, found that there are many people who have encountered such errors, the problem is straightforward, because in the PHP configuration file does not open
Start a parameter, on my local machine is/apache/bin/php.ini; Extension=php_openssl.dll This item requires the former
The semicolon of the face is removed. 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);
Running the above script fragment, 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 ("Compress.zlib")
[8]=>
String "Compress.bzip2"
[9]=>
String (4) "Phar"
}
Alternative Solutions
It is easy to find errors and correct errors, but it is difficult to find errors after they are found. I was going to put this script method far
Host, but I can't modify the remote host's PHP configuration, and as a result, I can't use this scheme, but we can't be on a tree
Hang, this road can't get through, see if there is any other way.
Another function I often use to crawl content in PHP is curl, which is more powerful than file_get_contents () and provides a lot of
Selected parameters. For the problem of accessing HTTPS content, the Curl configuration parameters we need to use are:
curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
As you can see semantically, it is ignoring/skipping SSL security authentication. Maybe it's not a good practice, but for a normal scenario
, that's enough.
Here is a function that uses the Curl package to 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;
}