標籤:
file_get_contents無法請求https串連的解決方案
方法1:
PHP.ini預設配置下,用file_get_contents讀取https的連結,就會如下錯誤:
Warning: fopen() [function.fopen]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?
解決方案有3:
1.windows下的PHP,只需要到php.ini中把extension=php_openssl.dll前面的;刪掉,重啟服務就可以了。
2.linux下的PHP,就必須安裝openssl模組,安裝好了以後就可以訪問了。
3.如果伺服器你不能修改配置的話,那麼就使用curl函數來替代file_get_contents函數,當然不是簡單的替換啊。還有相應的參數配置才能正常使用curl函數。
對curl函數封裝如下:
[php] view plaincopyprint?
- function http_request($url,$timeout=30,$header=array()){
- if (!function_exists(‘curl_init‘)) {
- throw new Exception(‘server not install curl‘);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- if (!empty($header)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- }
- $data = curl_exec($ch);
- list($header, $data) = explode("\r\n\r\n", $data);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if ($http_code == 301 || $http_code == 302) {
- $matches = array();
- preg_match(‘/Location:(.*?)\n/‘, $header, $matches);
- $url = trim(array_pop($matches));
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, false);
- $data = curl_exec($ch);
- }
-
- if ($data == false) {
- curl_close($ch);
- }
- @curl_close($ch);
- return $data;
- }
php開啟curl
開啟php curl函數庫的步驟
1).去掉windows/php.ini 檔案裡;extension=php_curl.dll前面的; /*用 echo phpinfo();查看php.ini的路徑*/
2).把php5/libeay32.dll,ssleay32.dll複製到系統目錄windows/下
3).重啟apache
配置php支援curl
curl是一個利用URL文法在命令列方式下工作的檔案傳輸工具。它支援很多協議:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同樣支援HTTPS認證,HTTP POST方法, HTTP PUT方法, FTP上傳, kerberos認證, HTTP上傳, Proxy 伺服器, cookies, 使用者名稱/密碼認證, 下載檔案斷點續傳, 上傳檔案斷點續傳, httpProxy 伺服器管道( proxy tunneling), 甚至它還支援IPv6, socks5Proxy 伺服器, 通過httpProxy 伺服器上傳檔案到FTP伺服器等等,功能十分強大。Windows作業系統下的網路螞蟻,網際快車(FlashGet)的功能它都可以做到。準確的說,curl支援檔案的上傳和下載,所以是一個綜合傳輸工具,但是按照傳統,使用者習慣稱curl為下載工具。
配置方法:
1、拷貝PHP目錄中的libeay32.dll 和 ssleay32.dll 兩個檔案到 system32 目錄。
2、修改php.ini:配置好 extension_dir ,去掉 extension = php_curl.dll 前面的分號。
---------------------------
php下擴充php_curl.dll的安裝
---------------------------
已經內建有php_curl.dll,在ext目錄下,此DLL用於支援SSL和zlib.
在php.ini中找到有extension=php_curl.dll, 去掉前面的注釋.
設定extension_dir=c:phpext, 重新整理PHP頁面時報錯, 說找不到模組php_curl.dll.
拷貝php_curl.dll 到windowssystem32,還是同樣的錯.
在網上找了一下,需要將:
libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll
都拷貝到system32目錄下,重啟IIS即可.
file_get_contents無法請求https串連的解決方案 php開啟curl