php语法如何获取上证指数的股票数据

2024年09月09日 建站教程

如何利用php语法获取上证指数(股票代码sh000001)的股票数据,下面web建站小编给大家简单介绍一下!

安装命令:

composer require php-http/guzzle6-adapter
composer require php-http/message

php代码:

$code = 'sh000001'; // 上证指数
$url = 'http://finance.sina.com.cn/realstock/company/' . $code . '/nc.shtml';
 
$client = new HttpAdapterGuzzle6Client();
$request = new HttpMessageRequest('GET', $url);
$response = $client->sendRequest($request);
 
$html = $response->getBody()->getContents();
$dom = new DOMDocument();
@$dom->loadHTML($html); // 解析HTML
 
$xpath = new DOMXPath($dom);
$name = $xpath->query('//h1[@class="name"]/text()')->item(0)->nodeValue;
$price = $xpath->query('//span[@class="price"]/text()')->item(0)->nodeValue;
 
echo $name . '的当前价格为' . $price;

本文链接:http://so.lmcjl.com/news/12606/

展开阅读全文
相关内容