Home > php教程 > php手册 > 深圳通余额查询的API

深圳通余额查询的API

WBOY
Release: 2016-06-06 19:35:34
Original
1570 people have browsed it

深圳通余额查询的API,可以通过深圳通号码查询到余额及卡有效期等信息。 用到Domxpath和Curl两方面的知识。 源码已托管到github,另外要加载个类:myclass 项目地址:https://github.com/skiy/dev 演示:http://api.oupag.com/dev/api/shenzhentong.php?cardn

深圳通余额查询的API,可以通过深圳通号码查询到余额及卡有效期等信息。
用到Domxpath和Curl两方面的知识。
源码已托管到github,另外要加载个类:myclass
项目地址:https://github.com/skiy/dev

演示:http://api.oupag.com/dev/api/shenzhentong.php?cardno=293005762

update 2014.10.20 22:53 
原来可以直接用GET方式,不需要用curl的POST方式来处理,这样响应快些~
PHP
<?php
/**
 * shenzhentong.php
 * 深圳通的API
 * @autuor: Skiychan
 * @contact: developer@zzzzy.com & QQ:1005043848
 * @website: www.zzzzy.com & http://weibo.com/ckiy
 * @date: 2014-10-19
 * @readme https://github.com/skiy/dev/blob/master/docs/shenzhentong.md
 */

/**
链接:http://query.shenzhentong.com:8080/sztnet/qrycard.jsp

接口信息
URL:http://query.shenzhentong.com:8080/sztnet/qryCard.do
     http://query.shenzhentong.com:8080/sztnet/qryCard.do?cardno=328375558
POST方法:cardno:328375558

### 返回字段 json格式
返回值字段 | 字段类型 | 字段说明
----|------|----
card_number   | int     | 卡号
card_balance  | string  | 卡内余额
balance_time  | string  | 余额截止时间
card_validity | string  | 卡有效期
current_time  | string  | 查询时间

 */

    require_once "../libs/myclass.php";

    date_default_timezone_set("Asia/Shanghai");

    $cardno = isset($_GET["cardno"]) ? $_GET["cardno"] : 0;
    $post_cardno = "cardno={$cardno}";
    $data = new Myclass();

    //curl 的POST方式
    //$page = $data->curls("http://query.shenzhentong.com:8080/sztnet/qryCard.do", false, $post_cardno);
    //直接GET方式
    $page = $data->curls("http://query.shenzhentong.com:8080/sztnet/qryCard.do?cardno={$cardno}");
    $page = $data->pageToDom($page, "GBK");

    $tr = $page->query("//table[@class='tableact']/tr/td");

    function getTextContent($m_query, $m_id) {
        $myTXT = str_replace(":", "", $m_query->item($m_id)->textContent);
        return $myTXT;
    }

    //截止时间内余额
    preg_match("/截止到([^\)]*)/", getTextContent($tr, 2), $expires);

    $results = array(
        "card_number" =>  (int) getTextContent($tr, 1),
        "card_balance" =>  getTextContent($tr, 3),
        "balance_time" => $expires[1],
        "card_validity" =>  getTextContent($tr, 5),
        "current_time" => date("Y-m-d H:i:s", time()));

    echo json_encode($results);

?>
 
Copy after login
<?php
/**
 * myclass.php
 * dev的基础类库
 * Autuor: Skiychan
 * Contact: developer@zzzzy.com & QQ:1005043848
 * Website: www.zzzzy.com & http://weibo.com/ckiy
 * Date: 2014-10-19
 */

class Myclass {

    /* curl配置: 取网页源码、模拟登陆、POST提交
     * @param $url: 如果非数组,则为http;如是数组,则为https
     * @param $header: 头文件
     * @param $post: post方式提交 array 或 abc=1&bcd=2 形式
	 * @param $cookies: 0默认无cookie,1为设置,2为获取
     */
	public function curls($urls, $header = FALSE, $post = FALSE, $cookies = 0) {
		$url = is_array($urls) ? $urls['0'] : $urls;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		 
		//带header方式提交
		if($header != FALSE){
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		}
		 
		//post提交方式
		if($post != FALSE){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}

        //cookies
		if($cookies == 1){
			curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
		}else if($cookies == 2){
			curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
		}

        //https
		if(is_array($urls)){
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		}
		
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}

    /* 将网页转换成XML,再转换成DOM
     * @param $data 非数组=>源码,数组 array($url, 1)1为file_get_contents,2为curl
     */
    public function pageToDom($data, $encoded = "utf-8"){

        if (is_array($data)) {
           if ($data[1] == 1) {
               $datas = @file_get_contents($data[0]);
           }

           if ($data[1] == 2) {
               $datas = @$this->curls($data[0]);
           }
        } else {
            $datas = $data;
        }

        if (empty($datas)){
            return false;
        }

        $meta = '<meta http-equiv="Content-Type" content="text/html; charset='.$encoded.'"/>';
        $datas = $meta.$datas;
        $xmldoc = new DOMDocument();
        @$xmldoc->loadHTML($datas);
        $xmldoc->normalizeDocument();
        $domresult = new Domxpath($xmldoc);

        return $domresult;
    }
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template