2011-02-22 75 views
10

我希望能夠得到此網址的返回數據。 我甚至可以在PHP中執行此操作嗎?在PHP中提取JSONP結果集

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

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

我相信這會返回一個Javascript回調函數,但我沒有線索從哪裏開始。

以下是返回的結果集示例。

YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"yahoo","Result":[{"symbol":"YHOO","name": "Yahoo! Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"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"}]}}) 

任何幫助,非常感謝。

回答

38

對,它是JSON with padding.您必須刪除函數名稱(和括號),然後您可以使用json_decode解析JSON。

我曾經寫了一個函數:

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); 
} 

用法:

$data = jsonp_decode($response); 

DEMO

-3

可以使用json_decode在PHP以變換的結果到一個數組(只剝離函數調用和設置)

2

首先嚐試一下,它可能爲您節省麻煩:只需簡單地忽略來自URLcallback參數。

如果已定義callback,則此webservice具有產生JSONP的相當標準的行爲;純JSON否則。嘗試:

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

返回純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

返回JSONP(JSON包裹在一個函數調用):

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"}]}});