Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use aggregated data API to query express data

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use aggregate data API to query express data, the article is very detailed, has a certain reference value, interested friends must read it!

Documents for this project

Because my friend's website is written in ThinkPHP, these three functions are also written in ThinkPHP in order to keep the code compatible in the future.

All the files of the project are placed on GitHub, and some sensitive data has been hidden. You need to replace them by yourself. The address is as follows:

GitHub address: use aggregated data API to query express data-SMS verification code-enterprise verification name

Because these three functions are not official products, they will need to be embedded in various functional modules of the website in the future, so in order to make it easy to view, the code of the three functions is written in one file, so you just need to focus on the following files:

/ Home/Conf/config.php parameter configuration file / Home/Controller/IndexController.class.php back-end code, API request and processing / Home/View/Index_index.html front-end html

Apply for KEY and read the development documentation

Go to the above two websites to find the document addresses of "express", "SMS" and "check name". According to the instructions inside, put KEY, URL and other information into the configuration file Home/Conf/config.php for later reuse.

Commonly used express API documents

SMS API document

Kernel name-document

Pay attention to the API service of SMS. You need to add a "SMS template" to the backend of the website to allow the administrator to review it before you can call it normally.

Finally, the Home/Conf/config.php configuration file is as follows:

Return array (/ / Express query 'EXPRESS_APP_KEY' = >' your express APPKEY', 'EXPRESS_QUERY_URL' = >' http://v.juhe.cn/exp/index', / / Express order number query 'EXPRESS_COM_URL' = >' http://v.juhe.cn/exp/com', / / Express company query / / send SMS 'SEND_SMS_KEY' = >' your SMS interface APPKEY' 'SEND_SMS_URL' = >' http://v.juhe.cn/sms/send', / / nuclear name 'COMPANY_KEY' = >' nuclear name 'COMPANY_URL' = >' http://eci.yjapi.com/ECIFast/Search', / / database configuration information 'DB_TYPE' = >' mysql', / / database type 'DB_HOST' = >' localhost' / / server address' DB_NAME' = > 'your database name', / / database name 'DB_USER' = >' your database user name', / / user name 'DB_PWD' = >' password', / / password 'DB_PORT' = > 3306, / / port' DB_PREFIX' = > 'pre_'' / / Database table prefix 'DB_CHARSET'= >' utf8', / / character set) Set up the database

In order to prevent malicious users from making trouble using the exposed SMS interface, the behavior of each mobile phone number needs to be recorded. For example:

After sending an SMS verification code to a mobile phone number, you cannot send it again within 60 seconds. You must enter the SMS verification code within 1 hour, otherwise it will expire to prevent the ajax request address from being used by the robot program. You need to use the image verification code in the form to submit data about "form verification code". The following code will explain. Here, the table structure is created as follows, which is used to record the sending record of SMS messages:

CREATE TABLE `pre_ smsrecord` (`id` int (11) unsigned NOT NULL AUTO_INCREMENT, `mobile` varchar (11) NOT NULL DEFAULT'', `tpl_ id` int (11) NOT NULL, `code` int (6) NOT NULL, `time` int (11) NOT NULL, PRIMARY KEY (`id`) ENGINE=M

Mobile is the mobile phone number tpl_id is added in the background of the website and approved by the SMS template code is sent CAPTCHA (usually 4 or 6 digits) time is sent timestamp download sql directly to restore: the GitHub address of this project can also be directly from the / Pubic directory to find the sql file, you can directly restore it to your MySQL.

Implementation of form CAPTCHA

Because all three functions require form CAPTCHA, implement it first.

Open / Home/View/Index_index.html and pay attention to the img tag of the image verification code and the corresponding javascript

(general) input CAPTCHA

Refresh

JQuery (document) .ready (function ($) {/ / Refresh CAPTCHA button $("# btn-refresh-verify") .click (function (event) {$("# verify-img"). Attr ('src',' /? src','/? time= + new Date (). GetTime ());})

The corresponding backend code is in / Home/Controller/IndexController.class.php

/ * generate verification code * * -- * / public function verify () {$Verify = new\ Think\ Verify () $Verify- > entry () } / * +-* check the verification code * @ param string $code entered by the verification code * @ return boolean* +- -- * / protected function check_verify ($code) {$verify = new\ Think\ Verify () Return $verify- > check ($code);} generic Curl method, which is used to initiate a request to the API of a third-party website and get the return value

Because the three functions actually request the API interface address of a third-party website through the network, it can be unified into a general method. The code is as follows. You can pass in three variables: url, parameter array, request method (whether it is post or not, default is 0), and return data in json format.

/ * General request API for "aggregated data" Returns the JSON data * * @ param string $url interface address * @ param array $params passed parameter * @ param int $ispost whether it is submitted as POST Default GET* @ return json* +-* / 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, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22'); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt ($ch, CURLOPT_TIMEOUT, 30) 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.'?'.$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;}

Later, we can call this general method by getting express data, sending text messages and querying the name of the enterprise.

The realization of getting express data list

Open / Home/View/Index_index.html

Get express data

Select the company Shunfeng Shentong Yuantong Yunda EMS Zhongtong Huitong

Enter the order number

Inquiry express delivery

Return the result:

Express dynamics:

JQuery (document) .ready (function ($) {/ / Click the express query button $("# btn-query-express") .click (function (event) {$("# reason"). Html (""); / / Update the CAPTCHA $("# verify-img"). Attr ('src',' /? attr ('src',' /? makeshift homemade indexers) verify` + "& time=" + new Date (). GetTime ()) Company: $("# express-company"). Val (), number: $("# verify"). Val (), verify: $("# verify"). Val (),}, function (json) TextStatus) {if (json ['resultcode'] = = 200) {var result_list = json [' result'] ['list'] $("# express-result"). Html ("); for (var I = 0, l = result_list.length; I)

< l; i++) { $("#express-result").append("" + result_list[i]['datetime'] + "," + result_list[i]['remark'] + "," +result_list[i]['zone'] + ""); } } $("#reason").html(json['reason']); }); });}); 对应的后端代码为如下,特别注意,你要把 this->

Juhecurl (C ("EXPRESS_QUERY_URL"), $params, 1); the comment on this paragraph is removed (because I didn't query the balance when I was developing, I used a dead array to make the program work properly)

/ * +-* obtain express data * * @ param string $get.company express company code * @ param string $get.number express order number * @ return json * +-* / public function getExpressData () {/ / pass in the get parameter Including company code, express order number, verification code $com = I ("get.company") $no = I ("get.number"); $verify = I ("get.verify"); / / processing CAPTCHA if (! $this- > check_verify ($verify)) {$content = array ('resultcode'= > 1000,' reason'= > 'CAPTCHA error'); echo json_encode ($content); exit () } / / processing robot program brush interface (currently judged by IP) $ip= get_client_ip (0, true); $Record = M ("expressrecord"); $express_record = $Record- > where ("ip='"). $ip. "'")-> find (); if ($express_record & & ((time ()-$express_record ['time']))

< 60 ) ){ echo json_encode(array('reason'=>

Can only be queried once in 60 seconds); exit ();} if ($com & & $no) {$params = array ('key' = > C ("EXPRESS_APP_KEY"),' com' = > $com, 'no' = > $no) / / the test phase of the development directly returns a value without requesting API / / $content = $this- > juhecurl (C ("EXPRESS_QUERY_URL"), $params, 1); $content = array ('resultcode'= > 200,' reason'= >' query success', 'result'= > array (' list'= > array (); / / Delete the old record (if any), and then add the new record $Record- > where ("ip='"). $ip. "'")-> delete (); $data = array ('ip' = > $ip,' time'= > time ()); $Record- > add ($data); / / $returnArray = json_decode ($content,true); echo json_encode ($content,true);}}

Transmission and verification of SMS verification code

Needless to say, the front-end html goes directly to the code. There are actually two actions here, one is "send a CAPTCHA to my mobile phone number xxxxx", and the other is "I have received it and filled it out. Please see if the CAPTCHA I filled in is correct".

Send SMS verification code and check

SMS template: apply for registration, get your password back, check your name online.

Mobile phone number:

Enter the mobile verification code: send the verification code

Submit the mobile phone verification code

/ * 60 seconds countdown after sending SMS verification code * / var seconds_left = 60 leading function sms_count_down () {if (seconds_left > 0) {seconds_left = seconds_left-1; / / var b=new Date (); document.getElementById ("sms-count-down") [xss_clean] = seconds_left + "seconds"; setTimeout ("sms_count_down ()", 1000) } else {/ / cannot set any value for setAttribute according to http://stackoverflow.com/questions/7526601/setattributedisabled-false-changes-editable-attribute-to-false / /, it will become "disabled" and use removeAttribute document.getElementById ("btn-send-sms"). RemoveAttribute ("disabled"); document.getElementById ("btn-send-sms") [xss_clean] = "resend" Document.getElementById ("sms-count-down") [xss_clean] = ";}} jQuery (document) .ready (function ($) {/ / send text message $(" # btn-send-sms ") .click (function (event) {$(" # reason "). Html (") GetJSON ('/? getJSON ('/? getJSON), {tplid: $("# send-sms-tplid"). Val (), mobile: $("# send-sms-mobile"). Val (),}, function (json, textStatus) {$("# reason") .html (json ['reason']) Var error_code = json ['error_code']; if (error_code = = 0) {/ / Test phase, can be checked for 60 seconds to repeat sending / / $("# btn-send-sms") .attr ("disabled", true) / / start the countdown sms_count_down ();} else {$("# btn-send-sms") .html ("resend");});}) / / check the SMS verification code $("# btn-check-sms") .click (function (event) {$("# reason"). Html (""); / / update the SMS verification code $("# verify-img"). Attr ('src',' /? time= homemade indexation indexer verifiy' + "& time=" + new Date (). GetTime () Tplid: $("# send-sms-tplid"). Val (), mobile: $("# send-sms-mobile"). Val (), code: $("# send-sms-code"). Val (), verify: $("# verify"). Val () }, function (json, textStatus) {$("# reason") .html (json ['reason']) });)

Also because there are two actions, the back end will be a little more complicated.

/ * request SMS API It will take 60 seconds to resend the * * @ param int $get.tplid SMS template id* @ param string $get.mobile Mobile number * @ return json* +-* / public function SendSMS () {$tpl_id = I ("get.tplid") / / SMS template id: register 5596 to retrieve the password 5602 online kernel name 5603$ mobile = I ("get.mobile"); / / Mobile number / / check the database record to see if $Record = M ("smsrecord") has been sent within 60 seconds; $where = array ('mobile' = > $mobile,' tpl_id' = > $tpl_id,); $sms_record = $Record- > where ($where)-> find () If ($sms_record & & ((time ()-$sms_record ['time']))

< 60 ) ){ echo json_encode(array('reason'=>

'cannot be sent multiple times within 60 seconds'); exit ();} / / if not sent within 60 seconds, send CAPTCHA text messages (6 random digits) $code = mt_rand (100000, 999999) $smsConf = array ('key' = > C ("SEND_SMS_KEY"), / / the APPKEY' mobile' = > $mobile you applied for, / / the mobile phone number 'tpl_id' = > $tpl_id, / / the SMS template ID you applied for, and modify' tpl_value' = >'# code#=' according to the actual situation. $code / / the template variable you set, modify the'# code#=1234&#company#= aggregate data 'according to the actual situation; / / in the test phase, set a "successfully sent" json string $content = $this- > juhecurl (C ("SEND_SMS_URL"), $smsConf, 1) without sending text messages. / / request to send an SMS message / / $content = json_encode (array ('error_code'= > 0,' reason'= > 'sent successfully'); if ($content) {$result = json_decode ($content,true); $error_code = $result ['error_code'] If ($error_code = = 0) {/ / status is 0, indicating that the SMS message was sent successfully / / the database stores the sending record, which is used to handle the countdown and input verification. First, delete the old record $Record- > where ("mobile="). $mobile)-> delete (); $data = array ('mobile' = > $mobile,' tpl_id'= > $tpl_id, 'code'= > $code,' time'= > time ()); $Record- > data ($data)-> add () / / echo "SMS message was sent successfully, SMS ID:". $result ['result'] [' sid'];} else {/ / status is not 0, indicating failure / / echo "SMS delivery failed (". $error_code. "):. $msg }} else {/ / returned abnormal content. The following can be modified according to business logic / / $result ['reason'] =' SMS failed';} echo $content } / * * +-* check whether the mobile phone verification code is filled in correctly * you can add more fields to register, Login and other forms * * @ param string $get.verify verification code * @ param string $get.mobile mobile phone number * @ param int $get.tplid SMS template ID* @ param int $get.code mobile phone received verification code * +- -* / public function checkSmsCode () {$verify = I ("get.verify") $tpl_id = I ("get.tplid"); / / SMS template id: register 5596 to retrieve the password 5602 online verification name 5603$ mobile = I ("get.mobile"); / / Mobile number $code = I ("get.code"); / / CAPTCHA received by the phone if (! $this- > check_verify ($verify)) {$content = array ('resultcode'= > 1000,' reason'= > 'CAPTCHA error') Echo json_encode ($content); exit ();} / / check the database record to see if the entered verification code is the same as the one previously sent to the phone via SMS API $Record = M ("smsrecord"); $where = array ('mobile' = > $mobile,' tpl_id' = > $tpl_id, 'code' = > $code,) $sms_record = $Record- > where ($where)-> find (); if ($sms_record) {echo json_encode (array ('reason'= >' SMS verification successful')); / / process the following programs (such as login, registration, etc.)} else {echo json_encode ('reason'= >' SMS verification code error');}} last is the enterprise verification name

Because this interface originally provides only one keyword per query, while a friend's website requires multiple keywords separated by "," each query, so you need to process the submitted data and those returned to the front end (cycle through multiple lists).

The front-end code is relatively simple

Enterprise audit name

Province: Shanghai

Enterprise audit name

Company query list:

0) {for (I in json_list) {var result_list = json_ list [I] .result; if (result_list.length > 0) {for (j in result_list) {/ / console.log (result_ list [j]) $("# company-result") .append ("name:" + result_ list [j] .Name + ", legal person:" + result_ list [j] .OperName + ", status:" + result_ list [j] .status + ") }}))

Back-end code

/ * +-* Enterprise name * this API http://eci.yjapi.com/ECIFast/Search only supports GET mode. So $param is a string form * * @ param string $get.province province * @ param string $get.companyName company name * @ return json* +-* / public function getCompanyName () { / / pass in the get parameter $province = I ("get.province") $companyName = I ("get.companyName"); $verify = I ("get.verify"); / / processing CAPTCHA if (! $this- > check_verify ($verify)) {$content = array ('resultcode'= > 1000,' reason'= > 'CAPTCHA error'); echo json_encode ($content); exit () } / / processing robot program brush interface (currently judged by IP) $ip= get_client_ip (0, true); $Record = M ("companyrecord"); $company_record = $Record- > where ("ip='"). $ip. "'")-> find (); / / if ($company_record & & ((time ()-$company_record ['time']))

< 60 ) ){ // echo json_encode(array('reason'=>

The enterprise name can only be queried once in 60 seconds); / / exit (); / /} $resultArray = array (); if ($province & & $companyName) {/ / delete the old record (if any), and then add the new record $Record- > where ("ip='". $ip. "'")-> delete (); / / loop multiple company names $companies = explode (",", $companyName); foreach ($companies as $key= > $value) {$params = "key=". C ("COMPANY_KEY"). "& province= {$province} & companyName= {$value}"; / / return the value directly in the development test phase without requesting API $content = $this- > juhecurl (C ("COMPANY_URL"), $params, 0); $returnArray = json_decode ($content,true); / / insert the new result $resultArray [] = $returnArray in a loop / / $content = array ('resultcode'= > 200,' reason'= >' query success', 'result'= > array (' list'= > array ());} $data = array ('ip' = > $ip,' time'= > time ()); $Record- > add ($data); $content = json_encode ($resultArray, true); echo json_encode ($content, true) }} the above is all the contents of the article "how to use aggregated data API to query express data". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report