PHP captures HTTPS content and error handling methods, and php captures https content

Source: Internet
Author: User

PHP captures HTTPS content and error handling methods, and php captures https content

Problem

An HTTPS problem occurred while 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("/http://blog.it985.com/");?>

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?

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, on my local machine, it is/apache/bin/php. in ini ;extension=php_openssl.dll Remove the semicolon.

You can use the following script to check the configuration of your PHP environment:

<?php$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:

<?phpopenssl: nohttp wrapper: yeshttps wrapper: nowrappers: 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. For HTTPS content access 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;}

Summary

The above is all the content of the HTTPS content and error handling method captured by PHP. I hope the content in this article will be helpful for you to learn or use PHP. If you have any questions, please leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.