Php example of vehicle violation query data, php example of vehicle Violation

Source: Internet
Author: User
Tags php example

Php example of vehicle violation query data, php example of vehicle Violation

It is convenient for car owners to know whether they have experienced traffic violations at any time, so as to avoid unnecessary losses caused by forgetting or handling tickets within the time limit. This code example is a call to the national vehicle violation query API Based on aggregated data. If you need it, you can look down.

Before use, you need:

Use: https://www.juhe.cn/docs/api/id/36to apply for an appkey for illegal Query

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

Header ('content-type: text/html; charset = UTF-8 '); include 'class. juhe. wz. php'; // introduce the file

Ii. configuration parameters

// Interface basic information configuration $ appkey = '************'; // key for querying violations you have applied for $ wz = new wz ($ appkey );

3. query the list of cities supported by traffic violations because the supported cities are occasionally updated but not frequently. You can cache the data, for example, update the data every three hours, you do not need to request an interface every time.

$ Wzcitys = $ wz-> getCitys (); // query all supported cities $ wzcitys = $ wz-> getCitys ('gd '); // query cities in a specified Province

The returned data format is as follows: (it is very important that some conditions are required for the next query violation. For specific field meanings, refer to the official interface documentation, where regist and registno can be ignored, is required in the old version)

{"Resultcode": "200", "reason": "successful responses", "result": [{"province": "Beijing", "province_code ": "BJ", "citys": [{"city_name": "Beijing", "city_code": "BJ", "abbr": "Beijing", "engine ": "1", "engineno": "0", "classa": "0", "class": "0", "classno": "0", "regist ": "0", "registno": "0"}], "error_code": 0}

4. querying vehicle violation information basically only supports querying small-sized vehicles in the city, so hpzl can be omitted.

// Query the vehicle violation information according to the required query conditions $ city = 'gd _ DG '; // city code, $ carno = 'guangdong S ***** '; // license plate number. $ engineno =' ***** 'is required. // The engine number, $ classno = ****** '; // The required city id. $ wzResult = $ wz-> query ($ city, $ carno, $ engineno, $ classno); if ($ wzResult ['error _ Code'] = 0) {if ($ wzResult ['result'] ['lists']) {foreach ($ wzResult ['result'] ['lists'] as $ key => $ w) {// The echo $ w ['region'] is modified according to the actual business requirements. "". $ w ['date']. "". $ w ['ac']. "". $ w ['fen']. "". $ w ['money']. "<br>" ;}} else {echo "no violation record for the car" ;}} else {// query failed echo $ wzResult ['error _ Code']. ":". $ wzResult ['reason '];}

V. Complete class. juhe. wz. php code

<! --? Php // + keys // | JuhePHP [no zuo no die] // + keys // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved. // + Author // | Author: Juhedata <info@juhe.cn --> // + ---------------------------------------- ---------------------------- // Aggregate data National violation interface call class // define class wz {private $ appkey = false; // apply for national violation query APPKEY private $ cityUrl = 'HTTP: // v.juhe.cn/wz/citys'; private $ wzUrl = 'HTTP: // v.juhe.cn/wz/query'; public function _ construct ($ appkey) {$ this-> appkey = $ appkey ;} /*** obtain the list of cities supported by the violation * @ return array */public function getCitys ($ provin Ce = false) {$ params = 'key = '. $ this-> appkey. "& format = 2"; $ content = $ this-> juhecurl ($ this-> cityUrl, $ params); return $ this-> _ returnArray ($ content );} /*** query vehicle violations ** @ param string $ city [city code] * @ param string $ carno [license plate number] * @ param string $ engineno [engine number] * @ param string $ classno [rack number] * @ return array returns violation information */public function query ($ city, $ carno, $ engineno = '', $ classno ='') {$ params = array ('key' => $ thi S-> appkey, 'city' => $ city, 'hphm' => $ carno, 'engineno' => $ engineno, 'classno' => $ classno ); $ content = $ this-> juhecurl ($ this-> wzUrl, $ params, 1); return $ this-> _ returnArray ($ content );} /*** convert JSON content into data and return * @ param string $ content [content] * @ return array */public function _ returnArray ($ content) {return json_decode ($ content, true);}/*** request interface returned content * @ param string $ url [requested URL] * @ param string $ p Arams [Request Parameters] * @ param int $ ipost [whether to use the POST form] * @ 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, 'juhedata'); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt ($ ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ ch, CURLOPT_RET URNTRANSFER, 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. '? '. $ 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 ;}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.