Извлечение набора результатов JSONP в PHP

Я хотел бы получить доступ к возвращенным данным этого URL-адреса. Могу ли я это сделать на PHP?

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

Любая помощь приветствуется.

Правильно, это JSON с дополнением. Вам нужно удалить имя функции (и скобки), а затем вы можете проанализировать JSON с помощью json_decode .

Я однажды написал для этого функцию:

 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

Сначала попробуйте это, это может сэкономить вам неприятности: просто опустите параметр callback из URL-адреса .

Этот веб-сервис имеет довольно стандартное поведение, приводящее к JSONP, если определяется callback ; простой JSON в противном случае. Попробуй:

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

возвращает 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

возвращает 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"}]}}); 

вы можете использовать json_decode в php, чтобы преобразовать результат в массив (просто разделите вызов функции и paranthesis)