php二次开发股票系统代码:腾讯股票数据接口地址、批量获取股票信息、转换为腾讯接口指定的股票格式
1、腾讯股票数据控制器
<?php
namespace app\index\controller;
use think\Model;
use think\Db;
const BASE_URL = 'http://aaaaaa.aaaaa.com'; //腾讯数据地址
class TencentStocks extends Home
{
//里面具体的方法
}
2、请求接口返回内容
function juhecurl($url, $params = false, $ispost = 0)
{
header('Content-Type:text/html; charset=gb_2312');
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '/' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
3、批量获取股票信息
*0: 不明
*1: 名字
*2: 代码
*3: 现价
*4: 昨日收
*5: 今日开
*31: 涨跌价
*32: 涨跌百分比
*33: 最高价
*34: 最低价
function getStockInfo($codes)
{
$codeStrings = "";
$obj = new self();
foreach ($codes as $item) {
$code = $obj->toFrontStockCode($item);
$codeStrings = $codeStrings . ',' . $code;
}
if (startwith($codeStrings, ",")) {
$codeStrings = substr($codeStrings, 1);
}
$params = array(
"q" => $codeStrings,//股票编号,上海股市以sh开头,深圳股市以sz开头如:sh601009
);
$paramstring = http_build_query($params);
$res = $obj->juhecurl(BASE_URL, $paramstring);
$result1 = explode(';', $res);
$data = array();
foreach ($result1 as $item) {
$item = explode('"', $item)[1];
$item1 = explode('~', $item);
$codeInfo = array(
"stockName" => $item1[1],
"stockCode" => $item1[2],
"currentPrice" => $item1[3] == 0 ? $item1[4] : $item1[3],
"differentialPercent" => $item1[32],
"differential" => $item1[31],
"yesterdayCollect" => $item1[4],
"todayOpen" => $item1[5]
);
$data[] = $codeInfo;
}
return $obj->array_iconv("gbk", "utf-8", $data);
// echo '<pre>';
// print_r($data);
// die();
}
4、第一个参数为当前编码,第二个参数为转换后的编码,第三个参数是一维或者二维数组
function array_iconv($in_charset, $out_charset, $arr)
{
foreach ($arr as $k => &$v) {
if (is_array($v)) {
foreach ($v as $kk => &$vv) {
$vv = iconv($in_charset, $out_charset, $vv);
}
} else {
$v = iconv($in_charset, $out_charset, $v);
}
}
return $arr;
}
5、截取指定两个字符之间的字符串
public function cut($begin, $end, $str)
{
$b = mb_strpos($str, $begin) + mb_strlen($begin);
$e = mb_strpos($str, $end) - $b;
return mb_substr($str, $b, $e);
}
6、转换为腾讯接口指定的股票格式
function toFrontStockCode($code)
{
if (startwith($code, "0") || startwith($code, "3")) {
return "sz" . $code;
} else if (startwith($code, "6")) {
return "sh" . $code;
}
return $code;
}
7、把stdClass Object 转成数组array
function object2array($array)
{
if (is_object($array)) {
$array = (array)$array;
}
if (is_array($array)) {
foreach ($array as $key => $value) {
$array[$key] = $this->object2array($value);
}
}
return $array;
}
需要 二次开发、安装部署的,可以QQ扫一扫:
原文地址:https://blog.csdn.net/csdndddsd/article/details/135848649
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!