Detailed explanation of php vehicle violation query data

墨辰丷
Release: 2023-03-28 19:22:01
Original
1950 people have browsed it

Aggregation data National vehicle violation data interface currently supports about 300 city violation inquiries and has been connected to tens of thousands of APPs. This article introduces an example of php vehicle violation query data. Friends in need can take a look.

It is convenient for car owners to know whether they have had traffic violations at any time, and to avoid unnecessary losses caused by forgetting or overdue violation tickets. This code example is based on the call of the national vehicle violation query API based on aggregated data. If necessary, you can read below.

Before use, you need:

Apply for an appkey for violation query through: https://www.juhe.cn/docs/api/id/36

1. Introduce the encapsulated request class class.juhe.wz.php

header('Content-type:text/html;charset=utf-8');
include 'class.juhe.wz.php'; //引入文件
Copy after login

2. Configuration parameters

//接口基本信息配置
$appkey = '**********'; //您申请的违章查询key
$wz = new wz($appkey);
Copy after login

3. Query the list of supported cities for violations. Since the supported cities will be updated from time to time, but not too frequently, you can cache these data. For example, if it is updated every 3 hours, there is no need to request the interface every time.

$wzcitys = $wz->getCitys(); //查询所有的支持城市
$wzcitys = $wz->getCitys('GD'); //查询指定省份下的城市
Copy after login

The returned data format is as follows: (very important, it involves some conditions required for querying violations in the next step. For specific field meanings, please refer to the official interface document, of which regist and registno can be ignored and are only required for older versions)

{
  "resultcode": "200",
  "reason": "成功的返回",
  "result": [
    {
      "province": "北京",
      "province_code": "BJ",
      "citys": [
        {
          "city_name": "北京",
          "city_code": "BJ",
          "abbr": "京",
          "engine": "1",
          "engineno": "0",
          "classa": "0",
          "class": "0",
          "classno": "0",
          "regist": "0",
          "registno": "0"
        }
      ]
    }
  ],
  "error_code": 0
}
Copy after login

4. Querying vehicle violation information is basically only supported in cities Small car query, so hpzl can be omitted.

//根据需要的查询条件,查询车辆的违章信息
$city = 'GD_DG'; //城市代码,必传
$carno = '粤S*****'; //车牌号,必传
$engineno = '****'; //发动机号,需要的城市必传
$classno = '*****'; //车架号,需要的城市必传
$wzResult = $wz->query($city,$carno,$engineno,$classno);
if($wzResult['error_code'] ==0){
  if($wzResult['result']['lists']){
    foreach($wzResult['result']['lists'] as $key =>$w){
      //以下就是根据实际业务需求修改了
      echo $w[&#39;area&#39;]." ".$w[&#39;date&#39;]." ".$w[&#39;act&#39;]." ".$w[&#39;fen&#39;]." ".$w[&#39;money&#39;]."<br>";
    }
  }else{
    echo "该车无违章记录";
  }
}else{
  //查询不成功
  echo $wzResult[&#39;error_code&#39;].":".$wzResult[&#39;reason&#39;];
}
Copy after login

5. Class.juhe.wz.php complete code

<!--?php
// +----------------------------------------------------------------------
// | JuhePHP [ NO ZUO NO DIE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Juhedata <info@juhe.cn-->
// +----------------------------------------------------------------------
 
//----------------------------------
// 聚合数据全国违章接口调用类
//----------------------------------
class wz{
  private $appkey = false; //申请的全国违章查询APPKEY
 
  private $cityUrl = &#39;http://v.juhe.cn/wz/citys&#39;;
 
  private $wzUrl = &#39;http://v.juhe.cn/wz/query&#39;;
 
  public function __construct($appkey){
    $this->appkey = $appkey;
  }
 
  /**
   * 获取违章支持的城市列表
   * @return array
   */
  public function getCitys($province=false){
    $params = &#39;key=&#39;.$this->appkey."&format=2";
    $content = $this->juhecurl($this->cityUrl,$params);
    return $this->_returnArray($content);
  }
 
  /**
   * 查询车辆违章
   * @param string $city   [城市代码]
   * @param string $carno  [车牌号]
   * @param string $engineno [发动机号]
   * @param string $classno [车架号]
   * @return array 返回违章信息
   */
  public function query($city,$carno,$engineno=&#39;&#39;,$classno=&#39;&#39;){
    $params = array(
      &#39;key&#39; => $this->appkey,
      &#39;city&#39; => $city,
      &#39;hphm&#39; => $carno,
      &#39;engineno&#39;=> $engineno,
      &#39;classno&#39;  => $classno
    );
    $content = $this->juhecurl($this->wzUrl,$params,1);
    return $this->_returnArray($content);
  }
 
  /**
   * 将JSON内容转为数据,并返回
   * @param string $content [内容]
   * @return array
   */
  public function _returnArray($content){
    return json_decode($content,true);
  }
 
  /**
   * 请求接口返回内容
   * @param string $url [请求的URL地址]
   * @param string $params [请求的参数]
   * @param int $ipost [是否采用POST形式]
   * @return string
   */
  public function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , &#39;JuheData&#39; );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    if( $ispost )
    {
      curl_setopt( $ch , CURLOPT_POST , true );
      curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
      curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
      if($params){
        curl_setopt( $ch , CURLOPT_URL , $url.&#39;?&#39;.$params );
      }else{
        curl_setopt( $ch , CURLOPT_URL , $url);
      }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
      //echo "cURL Error: " . curl_error($ch);
      return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
  }
}
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP WeChat Development Synchronous Fans

Common methods for PHP to obtain file extensions

Knowledge points related to user login in PHP (1)

##

The above is the detailed content of Detailed explanation of php vehicle violation query data. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template