file_get_contents無法請求https串連的解決方案 php開啟curl

來源:互聯網
上載者:User

標籤:

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?
  1. function http_request($url,$timeout=30,$header=array()){  
  2.         if (!function_exists(‘curl_init‘)) {  
  3.             throw new Exception(‘server not install curl‘);  
  4.         }  
  5.         $ch = curl_init();  
  6.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  7.         curl_setopt($ch, CURLOPT_HEADER, true);  
  8.         curl_setopt($ch, CURLOPT_URL, $url);  
  9.         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  
  10.         if (!empty($header)) {  
  11.             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
  12.         }  
  13.         $data = curl_exec($ch);  
  14.         list($header, $data) = explode("\r\n\r\n", $data);  
  15.         $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
  16.         if ($http_code == 301 || $http_code == 302) {  
  17.             $matches = array();  
  18.             preg_match(‘/Location:(.*?)\n/‘, $header, $matches);  
  19.             $url = trim(array_pop($matches));  
  20.             curl_setopt($ch, CURLOPT_URL, $url);  
  21.             curl_setopt($ch, CURLOPT_HEADER, false);  
  22.             $data = curl_exec($ch);  
  23.         }  
  24.   
  25.         if ($data == false) {  
  26.             curl_close($ch);  
  27.         }  
  28.         @curl_close($ch);  
  29.         return $data;  
  30. }  

 

 

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.