當我在瀏覽器中使用以下URL時,它會提示我下載帶有JSOn內容的文本文件。從URL獲取文件內容?
https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json
(點擊上面的網址看到下載的文件的內容)
現在我想創建一個PHP頁面。我希望當我調用這個php頁面時,它應該調用上面的URL並從文件中獲取內容(json格式)並在屏幕上顯示它。
我該怎麼做?
當我在瀏覽器中使用以下URL時,它會提示我下載帶有JSOn內容的文本文件。從URL獲取文件內容?
https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json
(點擊上面的網址看到下載的文件的內容)
現在我想創建一個PHP頁面。我希望當我調用這個php頁面時,它應該調用上面的URL並從文件中獲取內容(json格式)並在屏幕上顯示它。
我該怎麼做?
根據您的PHP配置,這可能是容易的使用:
$jsonData = json_decode(file_get_contents('https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json'));
但是,如果allow_url_fopen
是不是你的系統上啓用,您可以通過CURL讀取數據如下:
<?php
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$jsonData = json_decode(curl_exec($curlSession));
curl_close($curlSession);
?>
順便說一句,如果你只是想要原始的JSON數據,那麼只需刪除json_decode
即可。
使用file_get_contents
結合json_decode
和echo
。
$url = "https://chart.googleapis....";
$json = file_get_contents($url);
現在你可以呼應$ json的變量,如果你只是想顯示輸出,也可以對其進行解碼,並用它做什麼,就像這樣:
$data = json_decode($json);
var_dump($data);
別忘記:要獲取HTTPS內容,您的OPENSSL擴展應該在您的php.ini中啓用。 (how to get contents of site use HTTPS)
1)本地最簡單方法
<?php
echo readfile("http://example.com/"); //needs "Allow_url_include" enabled
//OR
echo include("http://example.com/"); //needs "Allow_url_include" enabled
//OR
echo file_get_contents("http://example.com/");
//OR
echo stream_get_contents(fopen('http://example.com/', "rb")); //you may use "r" instead of "rb" //needs "Allow_url_fopen" enabled
?>
2)更好的方法是CURL:
echo get_remote_data('http://example.com'); // GET request
echo get_remote_data('http://example.com', "var2=something&var3=blabla"); // POST request
//See Updates and explanation at: https://github.com/tazotodua/useful-php-scripts/
function get_remote_data($url, $post_paramtrs=false)
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
if($post_paramtrs)
{
curl_setopt($c, CURLOPT_POST,TRUE);
curl_setopt($c, CURLOPT_POSTFIELDS, "var1=bla&".$post_paramtrs);
}
curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0");
curl_setopt($c, CURLOPT_COOKIE, 'CookieName1=Value;');
curl_setopt($c, CURLOPT_MAXREDIRS, 10);
$follow_allowed= (ini_get('open_basedir') || ini_get('safe_mode')) ? false:true;
if ($follow_allowed)
{
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
}
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
curl_setopt($c, CURLOPT_REFERER, $url);
curl_setopt($c, CURLOPT_TIMEOUT, 60);
curl_setopt($c, CURLOPT_AUTOREFERER, true);
curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');
$data=curl_exec($c);
$status=curl_getinfo($c);
curl_close($c);
preg_match('/(http(|s)):\/\/(.*?)\/(.*\/|)/si', $status['url'],$link); $data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/|\/)).*?)(\'|\")/si','$1=$2'.$link[0].'$3$4$5', $data); $data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/)).*?)(\'|\")/si','$1=$2'.$link[1].'://'.$link[3].'$3$4$5', $data);
if($status['http_code']==200)
{
return $data;
}
elseif($status['http_code']==301 || $status['http_code']==302)
{
if (!$follow_allowed)
{
if (!empty($status['redirect_url']))
{
$redirURL=$status['redirect_url'];
}
else
{
preg_match('/href\=\"(.*?)\"/si',$data,$m);
if (!empty($m[1]))
{
$redirURL=$m[1];
}
}
if(!empty($redirURL))
{
return call_user_func(__FUNCTION__, $redirURL, $post_paramtrs);
}
}
}
return "ERRORCODE22 with $url!!<br/>Last status codes<b/>:".json_encode($status)."<br/><br/>Last data got<br/>:$data";
}
注意:它自動handes FOLLOWLOCATION問題+遠程URL是自動重新更正! (src =「./ imageblabla.png」--------> src =「http://example.com/path/imageblabla.png」)
pson GNU/Linux發行版服務器,你可能需要安裝php5-curl
軟件包才能使用它。
謝謝男人。非常有幫助。 – 2017-03-18 23:38:27
我正在使用file_get_contents來獲取內容,但當我回應它時,它不是JSON。它顯示了一些特殊字符。 – Awan 2011-04-02 10:44:05
@Awan你有沒有想過呢?我也看到特殊字符。 – jewel 2013-08-02 00:34:00