在 PHP 中提取 JSONP 结果集
Posted
技术标签:
【中文标题】在 PHP 中提取 JSONP 结果集【英文标题】:Extract JSONP Resultset in PHP 【发布时间】:2011-07-02 04:19:09 【问题描述】:我希望能够访问此 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"])
非常感谢任何帮助。
【问题讨论】:
【参考方案1】:没错,就是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
【讨论】:
【参考方案2】:先试试这个,它可能会为您省去麻烦:只需从 URL 中省略 callback
参数。
如果定义了callback
,则此Web 服务具有产生JSONP 的相当标准的行为;否则为纯 JSON。试试看:
http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo®ion=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®ion=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"]);
【讨论】:
以上是关于在 PHP 中提取 JSONP 结果集的主要内容,如果未能解决你的问题,请参考以下文章
休眠,PostgreSQL:无法提取结果集,SQLState 25P02
GraphQL 和从 Oracle DB 中提取数据:查询以结果集响应并且不进入 Graphql 解析器窗口