Google Finance API Call中的JSON中的领导斜线
-
12-10-2019 - |
题
我一直在使用Google Finance API成功收集一些库存信息。问题是在打电话后 http://www.google.com/finance/info?infotype=infoquoteall&q=[$tickerSymbol]
, ,Google返回的JSON //
在它之前添加,因此无法使用PHP编码字符串 json_encode()
. 。这 JSONLINT JSON验证器 确认 //
S无效。明显的解决方法是从JSON的开头剥离斜线。尽管如此,我仍然想知道为什么Google正在向正在返回的JSON添加Slashes。多余的斜线背后有目的吗?这是一个带有php的怪癖 json_encode()
当其他语言只是忽略额外字符时,什么时候?我在做事不正确吗?
这是请求结果的示例 http://www.google.com/finance/info?infotype=infoquoteall&q=AAPL
带有领先的斜线。
// [ {
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "340.65"
,"l_cur" : "340.65"
,"ltt":"4:00PM EST"
,"lt" : "Jan 18, 4:00PM EST"
,"c" : "-7.83"
,"cp" : "-2.25"
,"ccol" : "chr"
,"el": "345.20"
,"el_cur": "345.20"
,"elt" : "Jan 18, 5:45PM EST"
,"ec" : "+4.55"
,"ecp" : "1.34"
,"eccol" : "chg"
,"div" : ""
,"yld" : ""
,"eo" : ""
,"delay": ""
,"op" : "327.05"
,"hi" : "344.76"
,"lo" : "326.00"
,"vo" : "66.34M"
,"avvo" : "11.28M"
,"hi52" : "348.48"
,"lo52" : "190.25"
,"mc" : "313.75B"
,"pe" : "22.49"
,"fwpe" : ""
,"beta" : "1.38"
,"eps" : "15.15"
,"name" : "Apple Inc."
,"type" : "Company"
}
]
解决方案
对于那些寻求准备好的答案的人,这里是PHP的一个工作示例;将JSON清洗并转变为对象。值可以轻松提取。
第二个只是为了使其更棒,它会在访问页面时向您发送PubNub频道(Cron是您的朋友)。 PubNub消息可以通过JavaScript轻松接收,因此可以直播...
<?php
//Obtain Quote Info
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXDB:DAX');
//Remove CR's from ouput - make it one line
$json = str_replace("\n", "", $quote);
//Remove //, [ and ] to build qualified string
$data = substr($json, 4, strlen($json) -5);
//decode JSON data
$json_output = json_decode(utf8_decode($data));
// get the last price
$last = $json_output->l;
//Output Stock price .
echo 'DAX: ' . $last;
//////////////////////////////
// send it through pubnub //
//////////////////////////////
require_once('Pubnub.php');
// Publish and Subscribe Keys
$publish_key = 'demo';
$subscribe_key = 'demo';
$subscribe_key = false;
// Create Pubnub Object
$pubnub = new Pubnub( $publish_key, $subscribe_key, $secret_key );
// Publish !
$channel = 'quoteTheDax';
$timestamp = $pubnub->time();
$pubish_success = $pubnub->publish(array(
'channel' => $channel,
'message' => array("last" => $last2, "ts" => $timestamp)
));
//Boom its send to ppl subscribed to this channel arround the world
?>
当然,如果您需要实时更新的东西,则有更好的选择。我只是想每30分钟/60分钟更新一次。
其他提示
我想这是因为Google不希望您与该JSON合作,他们建议使用Google Data API。
不隶属于 StackOverflow