Php express Query API-support for various types of express delivery

Source: Internet
Author: User
You can enter the following delivery Name * (shentong-EMS-sf-Yuantong-Zhongtong-e.g. Fengda-Yunda-Tiantian-Huitong-Quanfeng-Debon-Jiasheng-aizhenda-package Ping-bang delivery) logistics * DHL Express-Daejeon logistics-Debon logistics-EMS domestic-EMS International-email Bao-Van Ke delivery-goutong express-registered letter-kickota-international parcel * Huitong express-Huayu logistics-Huiqiang Express -Jiaji express-Jiayi logistics-Canada Post



The express delivery API interface allows you to query multiple express delivery logistics ticket numbers. It can automatically identify express delivery companies.

Express Query API address: http://www.kuaidi.com/openapi.html



 _ APPKEY = $ key;}/*** sets the data return type. 0: return json string; 1: return xml object * @ param number $ show */public function setShow ($ show = 0) {$ this-> _ show = $ show ;} /*** set the number of logistics information entries returned. value 0: full information of multiple rows is returned. value 1: only one row of information * @ param number $ muti */public function setMuti ($ muti = 0) {$ this-> _ muti = $ muti;} is returned ;} /*** set the sorting of returned logistics information. Desc: Sort by time from new to old; asc: Sort by time from old to new * @ param string $ order */public function setOrder ($ order = 'desc ') {$ this-> _ order = $ order;}/*** query the logistics information and input the ticket number, * @ param logistics Bill No. $ nu * @ param company code $ com the courier company code to be queried. Chinese characters are not supported. for details, see the courier company code document. If this parameter is not specified, the system automatically matches the company according to the ticket number. Note: The success rate of single-digit matching is higher than 95%. * @ Throws Exception * @ return array */public function query ($ nu, $ com = '') {if (function_exists ('curl _ init ') = 1) {$ url = $ this-> _ APIURL; $ dataArr = array ('id' => $ this-> _ APPKEY, 'com' => $ com, 'nu '=> $ nu, 'show' => $ this-> _ show, 'muti' => $ this-> _ muti, 'order' => $ this-> _ order); foreach ($ dataArr as $ key => $ value) {$ url. = $ key. '= '. $ value. "&" ;}// echo $ url; $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_HEADER, 0 ); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ curl, CURLOPT_TIMEOUT, 10); $ kuaidresult = curl_exec ($ curl); curl_close ($ curl ); if ($ this-> _ show = 0) {$ result = json_decode ($ kuaidresult, true);} else {$ result = $ kuaidresult;} return $ result ;} else {throw new Exception ("Please install curl plugin", 1 );}}}


 SetShow (1); // Optional. the default value is 0. The return value is in json format. // The number of logistics information entries. 0: returns the complete information of multiple rows. 1: returns only one row. // $ kuaidichaxun-> setMuti (1); // optional, the default value is 0. // Set the sorting of the returned logistics information. Desc: Sort by time from new to old; asc: Sort by time from old to new // $ kuaidichaxun-> setOrder ('asc '); // query $ result = $ kuaidichaxun-> query ('201312', 'quanfengkuaid'); // query with company short code, for the short code list, see the documentation // $ result = $ kuaidichaxun-> query ('20140901', 'quanfengkuaidi '); // 111111 express waybill No. // quanfengkuaidi express delivery company name var_dump ($ result );



/**

* Express. class. php Express query
*
* @ Copyrightwiduu
* @ Licensehttp: // www.widuu.com
* @ Lastmodify2013-6-19
*/


Class Express {
 
Private $ expressname = array (); // encapsulate the express delivery name

Function _ construct (){
$ This-> expressname = $ this-> expressname ();
}

/*
* Webpage content collection method
*/
Private function getcontent ($ url ){
If (function_exists ("file_get_contents ")){
$ File_contents = file_get_contents ($ url );
} Else {
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
}
Return $ file_contents;
}
/*
* Method for obtaining the corresponding name and value passing
*/
Private function expressname (){
$ Result = $ this-> getcontent ("http://www.kuaidi100.com /");
Preg_match_all ("/data \-code \ = \"(? P \ w +) \ "\> \(? P. *) \ <\/span>/iU ", $ result, $ data );
$ Name = array ();
Foreach ($ data ['title'] as $ k => $ v ){
$ Name [$ v] = $ data ['name'] [$ k];
}
Return $ name;
}

/*
* How to parse an object into an array
* @ Param $ array of json input objects
* Return $ data array
*/
Private function json_array ($ json ){
If ($ json ){
Foreach (array) $ json as $ k => $ v ){
$ Data [$ k] =! Is_string ($ v )? $ This-> json_array ($ v): $ v;
}
Return $ data;
}
}

/*
* Returns the $ data array express array.
* @ Param $ name the express delivery name
*
* Express delivery can be achieved-such as Fengda-Swedish post-Quanyi Express-Quanfeng Express-quanshitong-Shentong Express-Shunfeng Express-Sauer express-TNT Express-Daily Express
* Tiandi Huayu-UPS Express-sinon logistics-singe logistics-Hong Kong Post-Yuantong Express-Yunda Express-postal parcel-Yousu express-Zhongtong Express)
* China Railway Express-home delivery-China Mail Logistics
* @ Param $ order the waybill number
* $ Data ['ischeck'] = 1 accepted
* $ Data ['data'] status array of real-time express query
*/
Public function getorder ($ name, $ order ){
$ Keywords = $ this-> expressname [$ name];
$ Result = $ this-> getcontent ("http://www.kuaidi100.com/query? Type = {$ keywords} & postid = {$ order }");
$ Result = json_decode ($ result );
$ Data = $ this-> json_array ($ result );
Return $ data;
}
}
$ A = new Express ();
$ Result = $ a-> getorder ("all-one Express", 111309582915 );
Var_dump ($ result );
?>

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.