2013-07-24 9 views

risposta

11

Questo è ciò che si dovrebbe fare:

$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 

Questo dovrebbe essere in grado di trasformare i dati JSON in un array.

Ora, per spiegare cosa fa.

file_get_contents() recupera essenzialmente il contenuto di un file, remoto o locale. Questo avviene attraverso il portale HTTP, quindi non stai violando la politica sulla privacy utilizzando questa funzione per il contenuto remoto.

Quindi, quando si utilizza json_decode(), normalmente si modifica il testo JSON in un oggetto in PHP, ma poiché abbiamo aggiunto true per il secondo argomento, restituisce invece un array associativo.

Quindi è possibile eseguire qualsiasi operazione con l'array.

Buon divertimento!

2

è necessario json_decode() la risposta e poi si deve come un array PHP per elaborarlo

2

Prima leggi la risposta in utilizzando curl. E poi, usa json_decode() per analizzare la risposta che hai usato con 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); 
Problemi correlati