1、问题原因
2022年1月21日以来,新浪股票接口更新后,原来的http://hq.sinajs.cn/list=sh600519接口访问返回403,直接网页访问返回提示“Kinsoku jikou desu!”(2022年11月份返回"Forbidden")
新浪股票的接口变动,需要在请求头中添加Referer值。
2、核心代码
<?php
$url = "http://hq.sinajs.cn/list=sh600519";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://finance.sina.com.cn/');
$output = curl_exec($ch);
$outputUtf8 = iconv('gb2312', 'utf-8', $output);
curl_close($ch);
echo $outputUtf8;
?>
参考:
https://blog.csdn.net/qq_45762148/article/details/123318800
https://blog.csdn.net/sunshineGGB/article/details/122983317
https://blog.csdn.net/qq1130169218/article/details/122809393