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
|