2011-04-02 125 views

回答

45

根據您的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即可。

2

使用file_get_contents結合json_decodeecho

+0

我正在使用file_get_contents來獲取內容,但當我回應它時,它不是JSON。它顯示了一些特殊字符。 – Awan 2011-04-02 10:44:05

+0

@Awan你有沒有想過呢?我也看到特殊字符。 – jewel 2013-08-02 00:34:00

1
$url = "https://chart.googleapis...."; 
$json = file_get_contents($url); 

現在你可以呼應$ json的變量,如果你只是想顯示輸出,也可以對其進行解碼,並用它做什麼,就像這樣:

$data = json_decode($json); 
var_dump($data); 
10

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軟件包才能使用它。

+0

謝謝男人。非常有幫助。 – 2017-03-18 23:38:27