0

我有以下文本,我想删除并获取数据。

(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;
?>

无论出于何种原因,我都无法弄清楚如何让另一个人使用我的代码。有人有什么建议吗?

4

2 回答 2

0

只需http://dev.markitondemand.com/Api/Quote/json?symbol=AAPL用作 url,在 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
        )

)*/
?>
于 2012-05-28T01:53:19.443 回答
-1

您用于 Google Finance 的代码是直接定制的,可以与您正在使用的 Google Finance 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: 属性中的任何内容。

于 2012-05-28T01:53:19.820 回答