您好,登錄后才能下訂單哦!
怎么在php中利用curl對股票信息進行查詢?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
class stock{ /** * 股票數據接口 */ const STOCK_URL = "http://apis.baidu.com/apistore/stockservice/stock"; /** * 通過拼音或者漢字獲取股票代碼 */ const SOCKET_SUGGEST = "http://cjhq.baidu.com/suggest?code5="; /** * 單態實例 */ private static $instance; /** * API 密鑰 */ private static $apikey; /** * 實例化類和指定API KEY * @param apikey string * @return instance object */ public static function getInstance($apikey){ if( self::$instance == NULL ){ self::$instance = new self; self::$apikey = $apikey; } return self::$instance; } /** * 獲取股票名稱 * @param stockid string * @return stockName string */ public static function getName($stockid){ $result = self::getSingleStock($stockid); return $result['name']; } /** * 獲取最后更新時間 * @param stockid string * @return time string */ public static function getTime($stockid){ $result = self::getSingleStock($stockid); return $result['date'].$result['time']; } /** * 獲取K線圖地址 * @param stockid string * @param date string min/day/week/mouth * @return imageUrl string */ public static function getKline($stockid,$date='min'){ $result = self::getSingleStock($stockid); return $result['klinegraph'][$date.'url']; } /** * 抓取整只股票的數據 * @param stockid string * @return stock infomation array */ public static function getSingleStock($stockid){ $type = preg_match('/(\d+){6}/is', $stockid); if ( $type == 0 ){ $stockid = self::getStockId($stockid); } $stock_url = self::STOCK_URL."?stockid=".$stockid; $result = self::httpGet( $stock_url , true ); if( $result['errNum'] != 0 ){ throw new Exception($result['errMsg'], 1); return; } return $result['retData']; } /** * 輸入拼音或者漢字來獲取股票代碼 * @param name string * @return stockid string */ private static function getStockId($name){ $result = self::httpGet( self::SOCKET_SUGGEST.urlencode(iconv('utf-8', 'GBK', $name)),false ); if (emptyempty($result)){ throw new Exception("stock name not exists", 2); return; } $stockid = $result['Result'][0]['code']; $stock = explode('.', $stockid); return $stock[1].$stock[0]; } /** * GET獲取方法 * @param param string 參數 * @author widuu */ private static function httpGet($url,$header=false) { $curlHandle = curl_init(); curl_setopt( $curlHandle , CURLOPT_URL, $url ); if( $header ){ curl_setopt( $curlHandle , CURLOPT_HTTPHEADER , array('apikey:'.self::$apikey)); } curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $curlHandle , CURLOPT_SSL_VERIFYPEER, false); curl_setopt( $curlHandle , CURLOPT_SSL_VERIFYHOST, false); curl_setopt( $curlHandle , CURLOPT_TIMEOUT, 10 ); $content = curl_exec( $curlHandle ); curl_close( $curlHandle ); return $header ? json_decode($content,true) :json_decode(iconv('GBK','utf-8',trim($content)),true); } } //測試代碼 stock::getInstance("5040bcbfebb0a4cffc7be278723255aa"); print_r(stock::getSingleStock('sh701000')); echo stock::getKline('紫金礦業');
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。