简体   繁体   中英

how to extract/session data from XML and/or JSON using PHP

I inserted the below command in developer.yahoo.com/yql/console/

yql = select * from yahoo.finance.quotes where symbol in ("XAUUSD=X","XAGUSD=X")

but show all the datas which are not necessary to me. XML Code Generated by Yahoo YQL and Json Code Generated by Yahoo YQL

i just want to pull out the Ask, Bid, AskRealtime, BidRealtime, LastTradeRealtimeWithTime, LastTradeWithTime, LastTradePriceOnly. By using php session, I want to insert the grabbed datas into html table/div in the same page.

Got no idea either XML would be easier to pull the data or Json. Which would be easier for me to grab the data of the fields and session it and paste it to the html table.

And also want the grabbed data to be streamable or auto refresh when datas are refreshed in yahoo ..
found this link
streamerapi.finance.yahoo.com

but got no idea how to work please help

i tried this but printed nothing

<script type='text/javascript'>  
  function forex(o){  
    var items = o.query.results.item;  
    var output = '';  
    var no_items=items.length;  
    for(var i=0;i<no_items;i++){  
      var Ask = items[i].Ask;
      var AskRealtime = items[i].AskRealtime;
      var BidRealtime = items[i].BidRealtime;
      var LastTradeDate = items[i].LastTradeDate;
      var LastTradePriceOnly = items[i].LastTradePriceOnly;
      var LastTradeTime = items[i].LastTradeTime;
      output += Ask + AskRealtime + BidRealtime + LastTradeDate + LastTradePriceOnly+ LastTradeTime;  
      +title + desc ; 
    }  
    // Place news stories in div tag  
    document.getElementById('results').innerHTML = output;    
  }  
</head>

check out json_decode(); ! works perfectly.

$x = json_decode($json_string);         // gives back an array
$x = json_decode($json_string, true);   // gives back an object

this thing has helped me a lot of times...

Use

http://api.jquery.com/jQuery.getJSON/

and put it on a timer. If you log the response (console.log) you'll see its an object that you can traverse through to get what you want.

It needs to be a valid json too, so make sure it is with JSON Lint:

http://jsonlint.com/

If it isn't (i think usually people prefix valid jsons with strings) then you'll have to use PHP to get the json (file_get_contents or CURL) and chop up the response until it's a valid json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM