2013-07-24 154 views

回答

11

這是你應該做的:

$data = file_get_contents(<url of that website>); 
$data = json_decode($data, true); // Turns it into an array, change the last argument to false to make it an object 

這應該是能夠把JSON數據到一個數組。

現在,解釋它的功能。

file_get_contents()本質上得到文件的內容,無論是遠程還是本地。這是通過HTTP門戶進行的,因此您不會通過將此功能用於遠程內容來違反隱私策略。

然後,當您使用json_decode()時,它通常會將JSON文本更改爲PHP中的某個對象,但由於我們爲第二個參數添加了true,它將返回一個關聯數組。

然後你可以對數組做任何事情。

玩得開心!

2

需要json_decode()的反應,然後你把它作爲一個PHP數組來處理它

2

首先閱讀使用curl的性反應。然後,使用json_decode()來解析你使用curl得到的響應。

2
// setup curl options 
    $options = array(
     CURLOPT_URL => 'http://serviceurl.com/api', 
     CURLOPT_HEADER => false, 
     CURLOPT_FOLLOWLOCATION => true 
    ); 

    // perform request 
    $cUrl = curl_init(); 
    curl_setopt_array($cUrl, $options); 
    $response = curl_exec($cUrl); 
    curl_close($cUrl); 

    // decode the response into an array 
    $decoded = json_decode($response, true);