我有以下文字要删除并抓取数据。
(function() {})({
"Data": {
"Status": "SUCCESS",
"Name": "Facebook Inc",
"Symbol": "FB",
"LastPrice": 31.91,
"Change": -1.12,
"ChangePercent": -3.39085679685135,
"Timestamp": "Fri May 25 16:00:05 UTC-04:00 2012",
"MarketCap": 20214729720,
"Volume": 37189630,
"ChangeYTD": 0,
"ChangePercentYTD": 0,
"High": 32.95,
"Low": 31.11,
"Open": 32.9
}
})
我还有以下代码与谷歌api合作,将于今年10月离开我们。
<?php
//Obtain Quote Info
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NASDAQ:' . $stock . '');
//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
$perc = $json_output->c;
$last = $json_output->l;
$date = $json_output->lt;
$name = $json_output->t;
?>
无论出于何种原因,我都无法弄清楚如何让对方使用我的代码。有人有什么建议吗?
答案 0 :(得分:0)
只需使用http://dev.markitondemand.com/Api/Quote/json?symbol=AAPL
作为网址,在json中省略p。
<?php
$stock = "FB";
//Obtain Quote Info
//$quote = file_get_contents('http://dev.markitondemand.com/Api/Quote/json?symbol='.$stock);
//or with curl
$quote = curl_get('http://dev.markitondemand.com/Api/Quote/json?symbol='.$stock);
function curl_get($url){
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
//decode JSON data
$json_output = json_decode(utf8_decode($quote));
$perc = $json_output->Data->ChangePercent;
$last = $json_output->Data->LastPrice;
$date = $json_output->Data->Timestamp;
$name = $json_output->Data->Name;
print_r($json_output);
/*
stdClass Object
(
[Data] => stdClass Object
(
[Status] => SUCCESS
[Name] => Facebook Inc
[Symbol] => FB
[LastPrice] => 31.91
[Change] => -1.12
[ChangePercent] => -3.3908567968514
[Timestamp] => Fri May 25 16:00:05 UTC-04:00 2012
[MarketCap] => 20214729720
[Volume] => 37189630
[ChangeYTD] => 0
[ChangePercentYTD] => 0
[High] => 32.95
[Low] => 31.11
[Open] => 32.9
)
)*/
?>
答案 1 :(得分:-1)
您用于Google财经的代码可以直接自定义,以使用您正在使用的Google财经API的输出。
以下是Google财经的输出:
// [
{
"id": "296878244325128"
,"t" : "FB"
,"e" : "NASDAQ"
,"l" : "31.91"
,"l_cur" : "31.91"
,"s": "0"
,"ltt":"4:00PM EDT"
,"lt" : "May 25, 4:00PM EDT"
,"c" : "-1.12"
,"cp" : "-3.39"
,"ccol" : "chr"
}
]
代码的一部分:
$data = substr($json, 4, strlen($json) -5);
正在从GF输出中删除不需要的字符。对于包含在匿名函数中的新输出,我建议如下:
<?php
preg_match('/\(function\(\) \{\}\)\((.*)\)/', $input_string, $matches);
$data_json = $matches[1];
$json_output = json_decode($data_json, true); //true = assoc array
$final_json = $json_output['Data'];
这将为$ json变量分配一个有效的json字符串,其中$ input_string是您在问题中列出的输入。 $ final_json将包含原始json的Data:属性中的任何内容。