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

  

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

安装命令:

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;
以上是编程学习网小编为您介绍的“php语法如何获取上证指数的股票数据”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。

相关文章