namespace app\index\controller;
use think\Model;
use think\Db;
const BASE_URL = 'http://aaaaaa.aaaaa.com'; //腾讯数据地址
class TencentStocks extends Home
{
//里面具体的方法
}
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;
}
*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 '';
// print_r($data);
// die();
}
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;
}
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);
}
function toFrontStockCode($code)
{
if (startwith($code, "0") || startwith($code, "3")) {
return "sz" . $code;
} else if (startwith($code, "6")) {
return "sh" . $code;
}
return $code;
}
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;
}