koalawangyang
koalawangyang
2017-09-27 07:59

PHP新浪股票接口使用问题

  • 新浪
  • 股票
  • php

http://hq.sinajs.cn/list=gb_psx,gb_aapl,gb_goog
通过这个URL可以获取飞利浦,苹果和谷歌当前的股票信息,获取后的信息如何以json形式存放再一个数组里面供前端html文件使用呢?

另外有哪位朋友知道如何能查询到伯克希尔哈撒韦的股票信息,
我试了http://hq.sinajs.cn/list=gb_brk.a brk.b brk-a brka等都查不出来。

我自己也写了一个php文件,但无法转换为json形式,前端说必须要json形式的,我也不是很懂。
<?php

error_reporting(0);
header('content-type:application:json;charset=utf8');

header('Access-Control-Allow-Origin:vonwayfx.com');

header('Access-Control-Allow-Methods:POST');

header('Access-Control-Allow-Headers:x-requested-with,content-type');

function stock_info($id){
if ($id == '') {
echo 'no stock id';
}
else {

$URL = "http://hq.sinajs.cn/list=".$id;
$result = file_get_contents($URL);
//$firstposition = stripos($result,"="); //get the first position of ','
$str1 = substr ($result,stripos($result,"=")+2,strlen($result)); //cut string before '
$str2 = substr ($str1,0,stripos($str1,";")-1);

$str_to_array = explode(',',$str2);
//var_dump (json_encode($str_to_array));
//$final = json_encode($str_to_array);
return $str_to_array$;
}
}
$ret = array(

0 => stock_info(gb_aapl),
1 => stock_info(gb_goog),
/* 's1' => stock_info( isset($_POST['s1'])? $_POST['s1'] : ''),

's2' => stock_info( isset($_POST['s2'])? $_POST['s2'] : ''),
's3' => stock_info( isset($_POST['s3'])? $_POST['s3'] : ''),
's4' => stock_info( isset($_POST['s4'])? $_POST['s4'] : ''),
's5' => stock_info( isset($_POST['s5'])? $_POST['s5'] : ''),
's6' => stock_info( isset($_POST['s6'])? $_POST['s6'] : ''),
's7' => stock_info( isset($_POST['s7'])? $_POST['s7'] : ''),
's8' => stock_info( isset($_POST['s8'])? $_POST['s8'] : ''),
's9' => stock_info( isset($_POST['s9'])? $_POST['s9'] : ''),
's10' => stock_info( isset($_POST['s10'])? $_POST['s10'] : ''), */
);

echo json_encode($ret);

?>

请大神帮忙看下,谢谢

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换