Domanda

Mi stavo chiedendo se c'era un modo per creare un parser in PHP in cui ottiene i valori da questo sito https://btc-e.com/api/2/btc_usd/ticker e li inserisce come variabili nel codice PHP?

Ho guardato un po 'i parser PHP e l'unica cosa che ho trovato è stato parser che riecheggia tutte le informazioni su un sito Web.

È stato utile?

Soluzione

Poiché quell'URL restituisce una risposta JSON:

<?php

$content=file_get_contents("https://btc-e.com/api/2/btc_usd/ticker");
$data=json_decode($content);
//do whatever with $data now
?>
.

Altri suggerimenti

È possibile utilizzare file_get_contents per ottenere i datiDall'URL e json_decode per analizzare il risultato, perché il sito che hai collegato è restituito a json array, che può essere analizzato da php nativamente.

Esempio:

$bitcoin = json_decode(file_get_contents("https://btc-e.com/api/2/btc_usd/ticker"), true);
.

Nella variabile $bitcoin avrai un array associativo con i valori della stringa JSON.

Risultato:

array(1) {
  ["ticker"]=>
  array(10) {
    ["high"]=>
    float(844.90002)
    ["low"]=>
    int(780)
    ["avg"]=>
    float(812.45001)
    ["vol"]=>
    float(13197445.40653)
    ["vol_cur"]=>
    float(16187.2271)
    ["last"]=>
    float(817.601)
    ["buy"]=>
    float(817.951)
    ["sell"]=>
    float(817.94)
    ["updated"]=>
    int(1389273192)
    ["server_time"]=>
    int(1389273194)
  }
}
.

I dati su quella pagina si chiama json ( JavaScript Object Notation ) (non è emerso come tipo JSON MIME, ma è formato come JSON).
Se you know che i dati saranno JSON, puoi acquisirlo come una stringa dalla pagina (usando ad esempio la funzione file_get_contents) e decodificarla in un array associativo con la funzione json_decode:

<?
function GetJsonFeed($json_url)
{
$feed = file_get_contents($json_url);
return json_decode($feed, true);
}
$LTC_USD = GetJsonFeed("https://btc-e.com/api/2/ltc_usd/ticker");
$LTC_USD_HIGH = $LTC_USD["ticker"]["last"];

$BTC_USD = GetJsonFeed("https://btc-e.com/api/2/btc_usd/ticker");
$BTC_USD_HIGH = $BTC_USD["ticker"]["last"];
?>
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top