PHP and HTTPS

Source: Internet
Author: User

Recently encountered an HTTPS problem while studying the hacker News API. Because all hacker News APIs are accessed through an encrypted HTTPS protocol, unlike the normal HTTP protocol, when using PHP's function file_get_contents () to get the data provided in the API, an error occurs, using the code: 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"} Alternatives find errors and correct errors, which is simple and difficult because errors cannot be corrected after they are found. I originally wanted to put this script method on the remote host, but I could not modify the remote host's PHP configuration, as a result, I could not use this scheme, but we can not be hanged in a tree, this road does not go through, see if there is no 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 optional parameters. For the problem of accessing HTTPS content, we need to use the Curl configuration parameter is: curl_setopt ($ch, curlopt_ssl_verifypeer,false); You can semantically see that it is ignoring/skipping SSL security authentication. It may not be a good idea, but it's enough for a normal scenario. 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;} Receive your free lamp brother and original PHP video tutorialCD/PHP Essentials edition, Details Inquiry official website customer Service: http://www.lampbrother.nethttp://yun.itxdl.cn/online/cto/index.php?u=5 This is a course for cattle x. CTO Course http://yun.itxdl.cn/online/server/index.php?u=5 Mobile Internet server-side development course Http://yun.itxdl.cn/online/weixin/index.php?u =5 Development Course http://yun.itxdl.cn/online/yingxiao/index.php?u=5 Micro Marketing course http://yun.itxdl.cn/online/phpcms/index.php?u= 5PHPCMS Two-time development course
  • 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.