PHP captures HTTPS content and error-handling methods _javascript Tips

Source: Internet
Author: User
Tags curl error handling openssl wrapper wrappers

Problem

An HTTPS problem was encountered while studying the hacker News API. Because all the hacker News APIs are accessed through the encrypted HTTPS protocol, unlike the normal HTTP protocol, when using PHP functions file_get_contents() to get the data provided in the API, there is an error

The code used is this:

<?php
$data = file_get_contents ("/http://blog.it985.com/");
? >

When running the code above, you are encountering the following error message:

PHP warning:file_get_contents (): Unable to find the wrapper "https" –did your forget to enable it when you configured PHP ?

Why is there such a mistake?

After a search on the internet, found that a lot of people have encountered such a mistake, the problem is very direct, because in the PHP configuration file does not open a parameter, in My computer is/apache/bin/php.ini in ;extension=php_openssl.dll this item, need to remove the semicolon before.

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);

Running the script fragment above, the result on my machine is:

<?php
openssl:no
http wrapper:yes
https wrapper:no
Wrappers:array (a) {
[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 scenarios

Finding errors, correcting errors, is simple and difficult to correct when you find an error. I originally wanted to put this script method on the remote host, but I couldn't modify the remote host's PHP configuration, and as a result, I couldn't use this option, but we couldn't hang in a tree, this road doesn't work, see if there's another way.

Another function that I often use to crawl content in PHP is curl that it is file_get_contents() more powerful and provides a lot of optional parameters. For problems accessing HTTPS content, the configuration parameters we need to use CURL are:

curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);

You can see from the semantics that it is ignoring/skipping SSL security validation. Perhaps this is not a good practice, but for the ordinary scene, this is enough.

Here is a function that leverages the Curl encapsulation 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;
}

Summarize

The above is PHP crawl HTTPS content and error handling of all content, I hope the content of this article for everyone to learn or use PHP can help, if there is doubt you can message exchange.

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.