簡體   English   中英

file_get_contents在我的服務器上不起作用

[英]file_get_contents not working on my server

我創建了兩個文件,一個在我的wamp服務器(localhost)上,另一個在我的ovh.com私有空間上。 這兩個文件只包含以下內容:

echo $search = file_get_contents('https://prod.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/19319907/recent?api_key=6fa73a35-6477-412d-97a6-b6739cb6cf1b');

在我的服務器上,有一些錯誤的字符,如â,þ或¬等...

它是如何發生的,我該如何解決這個問題?

編輯:這不是關於文件! 這是關於服務器,原因文件是完全一樣的! 你可以在這里檢查好的: http//www.dietadom.fr/test.php ,這里有一個壞的: httphttp://82.124.50.144/test.php

從請求到這兩個腳本的標頭:

工作:

curl -I  http://www.dietadom.fr/test.php
HTTP/1.1 200 OK
Set-Cookie: clusterBAK=R1564861669; path=/; expires=Wed, 19-Mar-2014 16:38:43 GMT
Date: Wed, 19 Mar 2014 15:19:31 GMT
Content-Type: text/html
Connection: keep-alive
Set-Cookie: cluster=R1649376954; path=/; expires=Wed, 19-Mar-2014 16:32:22 GMT
Server: Apache
X-Powered-By: PHP/5.4.24
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Vary: Accept-Encoding

不工作:

curl -I http://82.124.50.144/test.php
HTTP/1.1 200 OK
Date: Wed, 19 Mar 2014 15:19:48 GMT
Server: Apache/2.4.4 (Win64) OpenSSL/1.0.1d PHP/5.4.12
X-Powered-By: PHP/5.4.12
Content-Type: text/plain; charset: UTF-8

很可能兩個服務器提供的默認字符編碼頭是不同的。 您可以通過更改服務器配置來確保它們都具有默認值來解決此問題。 或者您可以修改您的腳本以覆蓋它,添加內容編碼標題將使這一點保持一致。

如果您修改PHP文件以獲得UTF-8普通測試內容,那么該行就是

header('Content-type: text/plain; charset=UTF-8');

您的源網址正在響應:

Content-Type: application/json; charset=UTF-8

檢索使用

curl -I https://prod.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/19319907/recent?api_key=6fa73a35-6477-412d-97a6-b6739cb6cf1b

因此我怎么知道它是UTF-8編碼的。

編輯

檢查過您的服務器后,我可以看到錯誤頁面的標題:

curl -I http://82.124.50.144/404.html
HTTP/1.1 403 Forbidden
Date: Wed, 19 Mar 2014 17:24:17 GMT
Server: Apache/2.4.4 (Win64) OpenSSL/1.0.1d PHP/5.4.12
Content-Type: text/html; charset=iso-8859-1

顯示默認字符編碼的最后一行是iso-8859-1,這意味着來自源的utf-8數據使用錯誤的編碼進行傳輸。 在腳本中添加正確的標題行可以解決問題。

您也可以通過添加AddDefaultCharset UTF-8來更改Apache服務器配置。

我確定你的問題是字符編碼,所以你應該研究一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM