开发者

Extract JSONP Resultset in PHP

I would like to be able to get to the returned data of this url. Can I even do this in PHP?

    <?php
    $yahooSS = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback";

    $yss = fopen($yahooSS,"r");
    ....

I believe this returns a Javascript callback function, but I don't have a clue where to start.

Below is an example of the returned Resultset.

YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"yahoo","Result":[{"symbol":"YHOO","name": "Yahoo! Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol开发者_JAVA百科":"YAHOY.PK","name": "YAHOO JAPAN CORP","exch": "PNK","type": "S","exchDisp":"Pink Sheets","typeDisp":"Equity"},{"symbol":"ETD","name": "Citigroup Inc. ELKS On Yahoo","exch": "PCX","type": "S","typeDisp":"Equity"},{"symbol":"YOJ.BE","name": "YAHOO JAPAN","exch": "BER","type": "S","exchDisp":"Berlin","typeDisp":"Equity"},{"symbol":"YHO.SG","name": "YAHOO","exch": "STU","type": "S","exchDisp":"Stuttgart","typeDisp":"Equity"},{"symbol":"YAHOF.PK","name": "YAHOO JAPAN CORP","exch": "PNK","type": "S","exchDisp":"Pink Sheets","typeDisp":"Equity"},{"symbol":"YHO.HM","name": "YAHOO","exch": "HAM","type": "S","exchDisp":"Hamburg","typeDisp":"Equity"},{"symbol":"YOJ.DE","name": "YAHOO JAPAN","exch": "GER","type": "S","exchDisp":"XETRA","typeDisp":"Equity"},{"symbol":"YHO.DU","name": "YAHOO","exch": "DUS","type": "S","exchDisp":"Dusseldorf Stock Exchange","typeDisp":"Equity"},{"symbol":"YHOO.BA","name": "YAHOO  INC.","exch": "BUE","type": "S","exchDisp":"Buenos Aires","typeDisp":"Equity"}]}})

Any help is greatly appreciated.


Right, it is JSON with padding. You have to remove the function name (and parenthesis) and then you can parse the JSON with json_decode.

I once wrote a function for that:

function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
    if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
       $jsonp = substr($jsonp, strpos($jsonp, '('));
    }
    return json_decode(trim($jsonp,'();'), $assoc);
}

Usage:

$data = jsonp_decode($response);

DEMO


Try this first, it might save you trouble: simply omit the callback parameter from the URL.

This webservice has the fairly standard behavior of yielding JSONP if callback is defined; plain JSON otherwise. Try it:

http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&region=us&lang=eng

returns plain JSON:

{"ResultSet":{"Query":"yahoo","Result":[{"symbol":"YHO.HM","name":"YAHOO","exch":"HAM","type":"S","exchDisp":"Hamburg","typeDisp":"Equity"},{"symbol":"YHOO","name":"Yahoo! Inc.","exch":"NAS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"YAHOY","name":"Yahoo Japan Corporation","exch":"PNK","type":"S","exchDisp":"OTC Markets","typeDisp":"Equity"},{"symbol":"YHOO.MX","name":"Yahoo! Inc.","exch":"MEX","type":"S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"YAHOF","name":"Yahoo Japan Corporation","exch":"PNK","type":"S","exchDisp":"OTC Markets","typeDisp":"Equity"},{"symbol":"YHOO.BA","name":"Yahoo! Inc.","exch":"BUE","type":"S","exchDisp":"Buenos Aires","typeDisp":"Equity"},{"symbol":"YHO.F","name":"YAHOO","exch":"FRA","type":"S","exchDisp":"Frankfurt","typeDisp":"Equity"},{"symbol":"YHO.MU","name":"YAHOO","exch":"MUN","type":"S","exchDisp":"Munich","typeDisp":"Equity"},{"symbol":"YHO.DE","name":"Yahoo! Inc.","exch":"GER","type":"S","exchDisp":"XETRA","typeDisp":"Equity"},{"symbol":"YHOO.F","name":"YAHOO","exch":"FRA","type":"S","exchDisp":"Frankfurt","typeDisp":"Equity"}]}}

http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&region=us&lang=eng&callback=YAHOO.Finance.SymbolSuggest.ssCallback

returns JSONP (JSON wrapped in a function call):

YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"yahoo","Result":[{"symbol":"YHO.HM","name":"YAHOO","exch":"HAM","type":"S","exchDisp":"Hamburg","typeDisp":"Equity"},{"symbol":"YHOO","name":"Yahoo! Inc.","exch":"NAS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"YAHOY","name":"Yahoo Japan Corporation","exch":"PNK","type":"S","exchDisp":"OTC Markets","typeDisp":"Equity"},{"symbol":"YHOO.MX","name":"Yahoo! Inc.","exch":"MEX","type":"S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"YAHOF","name":"Yahoo Japan Corporation","exch":"PNK","type":"S","exchDisp":"OTC Markets","typeDisp":"Equity"},{"symbol":"YHOO.BA","name":"Yahoo! Inc.","exch":"BUE","type":"S","exchDisp":"Buenos Aires","typeDisp":"Equity"},{"symbol":"YHO.F","name":"YAHOO","exch":"FRA","type":"S","exchDisp":"Frankfurt","typeDisp":"Equity"},{"symbol":"YHO.MU","name":"YAHOO","exch":"MUN","type":"S","exchDisp":"Munich","typeDisp":"Equity"},{"symbol":"YHO.DE","name":"Yahoo! Inc.","exch":"GER","type":"S","exchDisp":"XETRA","typeDisp":"Equity"},{"symbol":"YHOO.F","name":"YAHOO","exch":"FRA","type":"S","exchDisp":"Frankfurt","typeDisp":"Equity"}]}});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜