首頁 php教程 php手册 file_get_contents无法请求https连接的解决方法

file_get_contents无法请求https连接的解决方法

May 25, 2016 pm 04:45 PM
file_get_contents

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模块,安装好了以后就可以访问了.

安装方法:

OpenSSL库的安装

官网:http://www.openssl.org

下载页面:http://www.openssl.org/source/

选择最新版本下载

http://www.openssl.org/source/openssl-1.0.0a.tar.gz

解压:tar –zxvf openssl-1.0.0d.tar.gz,解压目录为:openssl-1.0.0d

然后进入到 cd openssl-1.0.0d,进行配置、编译、安装

配置: ./configure或./config

编译

make

安装

make install

3.如果服务器你不能修改配置的话,那么就使用curl函数来替代file_get_contents函数,当然不是简单的替换啊,还有相应的参数配置才能正常使用curl函数.

对curl函数封装如下:

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 (!emptyempty($header)) { 
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
	} 
	$data = curl_exec($ch); 
	list($header, $data) = explode("rnrn", $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; 
}
登入後複製

               
               

文章网址:

随意转载^^但请附上教程地址。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何解決PHP Warning: file_get_contents(): Filename cannot be empty 如何解決PHP Warning: file_get_contents(): Filename cannot be empty Aug 18, 2023 pm 07:30 PM

如何解決PHPWarning:file_get_contents():Filenamecannotbeempty在進行PHP開發的過程中,我們常常會遇到這樣的錯誤提示:PHPWarning:file_get_contents():Filenamecannotbeempty。這個錯誤通常出現在使用file_get_contents函數時

如何解決PHP警告:file_get_contents():無法開啟串流:HTTP請求失敗 如何解決PHP警告:file_get_contents():無法開啟串流:HTTP請求失敗 Aug 18, 2023 pm 11:34 PM

如何解決PHPWarning:file_get_contents():failedtoopenstream:HTTPrequestfailed在進行PHP開發過程中,常常會遇到透過file_get_contents函數向遠端伺服器發起HTTP請求的情況。然而,有時候我們會遇到一個常見的錯誤提示:PHPWarning:file_get_c

PHP檔案快取函數詳解:file_get_contents、file_put_contents、unlink等函數的檔案快取處理方法 PHP檔案快取函數詳解:file_get_contents、file_put_contents、unlink等函數的檔案快取處理方法 Nov 18, 2023 am 09:37 AM

PHP檔案快取函數詳解:file_get_contents、file_put_contents、unlink等函數的檔案快取處理方法,需要特定程式碼範例在Web開發中,我們經常需要從檔案讀取資料或將資料寫入到檔案中。而且,在某些情況下,我們需要快取文件的內容以避免頻繁的文件讀寫操作,從而提高效能。在PHP中,有幾個常用的函數可以幫助我們實現文件緩存,這其中包

PHP的file_get_contents()函數:如何從檔案讀取內容 PHP的file_get_contents()函數:如何從檔案讀取內容 Nov 04, 2023 pm 01:43 PM

PHP的file_get_contents()函數:如何從檔案讀取內容,具體程式碼範例在PHP中,file_get_contents()是一個非常有用的函數,它允許我們從檔案中讀取內容。無論是讀取文字文件,或是讀取遠端URL中的內容,該函數都能夠輕鬆地完成任務。語法函數的基本語法如下:stringfile_get_contents(string$f

PHP函數介紹—file_get_contents(): 讀取URL的內容到字串 PHP函數介紹—file_get_contents(): 讀取URL的內容到字串 Jul 24, 2023 pm 02:32 PM

PHP函數介紹—file_get_contents():讀取URL的內容到字串在Web開發中,經常需要從遠端伺服器取得資料或讀取遠端檔案。 PHP提供了一個非常強大的函數file_get_contents(),它可以方便地讀取URL的內容並將其保存到字串中。本文將介紹file_get_contents()函數的用法,並給出一些程式碼範例來幫助讀者更好

如何使用PHP中的file_get_contents函數讀取檔案內容 如何使用PHP中的file_get_contents函數讀取檔案內容 Jun 26, 2023 pm 12:01 PM

在PHP中,我們常常需要從檔案中讀取資料。在這種情況下,我們可以使用file_get_contents函數。這個函數可以簡單地從一個檔案中讀取所有內容,並將其作為一個字串傳回。這在許多場景下都非常有用,例如讀取設定檔、讀取日誌檔案等。在本文中,我們將介紹如何使用PHP中的file_get_contents函數來讀取檔案內容。步驟1:開啟檔案在使用file

PHP函數介紹—file_get_contents(): 讀取檔案內容到字串 PHP函數介紹—file_get_contents(): 讀取檔案內容到字串 Jul 24, 2023 pm 06:40 PM

PHP函數介紹—file_get_contents():讀取檔案內容到字串在PHP開發中,經常需要讀取檔案的內容並處理。要實現這項功能,可以使用PHP內建的函數file_get_contents()。本文將介紹file_get_contents()函數,並提供一些程式碼範例以幫助讀者更好地理解其用法。 file_get_contents函數的基本介紹fi

PHP 5.2函數詳解:如何使用file_get_contents函數讀取檔案內容 PHP 5.2函數詳解:如何使用file_get_contents函數讀取檔案內容 Jul 29, 2023 pm 04:09 PM

PHP5.2函數詳解:如何使用file_get_contents函數讀取檔案內容在PHP開發中,我們常常需要讀取檔案的內容。而PHP提供了許多方法來讀取檔案內容,其中一個常用且功能強大的函數是file_get_contents()。這個函數可以從一個檔案中讀取其內容,並將內容以字串的形式傳回,方便我們進行後續的處理。 file_get_contents()函

See all articles