12306 The implementation of the remaining ticket query and price query functions will be introduced in this article.
<?php /** * 车票接口类 * * @author chepiao100 * */ class chepiao100 { /** * 接口地址 * @var string */ private $_apiurl = 'https://www.chepiao100.com/api/'; /** * 返回接口数据 * * @param string $method 接口方法 * @param array $param 请求参数 * @return mixed */ function getData($method, $param) { $post = http_build_query($param); $html = $this->fetch_html($this->_apiurl.$method, $post); $jsonArr = json_decode($html, TRUE); if ( $jsonArr['errMsg'] == 'Y') { return $jsonArr['data']; } else { return $jsonArr['errMsg']; } } /** * 请求HTTP * * @param string $url * @param string $post * @return mixed */ function fetch_html($url, $post) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); //curl_setopt($ch, CURLOPT_PROXY, 'https://10.100.10.100:3128'); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $html = curl_exec($ch); curl_close($ch); return $html; } } /** End class of chepiao100 **/
This article introduces the 12306 remaining ticket query and price query functions, and uses code to implement related functions. For more related content, please pay attention to the PHP Chinese website.
Related recommendations:
Introduction to PHP related tutorials for quickly exporting Table data
Explain the use of ArrayAccess, the PHP predefined interface Method
Introduces PHP file naming, class and method naming, variable naming and other specifications
The above is the detailed content of PHP code implements 12306 remaining ticket query and price query functions. For more information, please follow other related articles on the PHP Chinese website!