/
kuaidi.class.php
91 lines (80 loc) · 2.24 KB
/
kuaidi.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
//688624242254 shunfeng
class Kuaidi {
//para
static $ch = null;
var $number = 0;
var $comCode = array();
//const
const UA = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0';
const HOST = 'www.kuaidi100.com';
const REFER = 'http://www.kuaidi100.com/';
//function construct & destruct
function __construct($num) {
$this->number = $num;
$this->create_obj();
}
function __destruct() {
$this->destroy_obj();
}
private function create_obj() {
if(self::$ch == null) {
self::$ch = curl_init();
$host = array("Host: ".self::HOST);
curl_setopt_array(self::$ch, array(
//debug start
//debug end
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_USERAGENT => self::UA,
CURLOPT_REFERER => self::REFER,
CURLOPT_HTTPHEADER => $host
));
}
}
private function destroy_obj() {
curl_close(self::$ch);
self::$ch = null;
}
//functions
//
//Choose company
public function query_com() {
curl_setopt_array(self::$ch, array(
CURLOPT_URL => 'http://www.kuaidi100.com/autonumber/autoComNum?text='.$this->number ,
CURLOPT_POST => 1
));
$response = curl_exec(self::$ch);
$response = json_decode($response, true);
$coms = $response['auto'];
$this->comCode = array();
foreach ($coms as $com) {
$this->comCode[] = $com['comCode'];
}
return $this->comCode;
}
//Query number with that comCode
public function query_num($comCode = false) {
//?type=shunfeng&postid=688624242254&id=1&valicode=&temp=0.5323619142104129
if($comCode) {
$query = '?type='.$comCode.'&postid='.$this->number.'&valicode=&temp='.random_float();
}else {
$query = '?type='.$this->comCode[0].'&postid='.$this->number.'&valicode=&temp='.random_float();
}
curl_setopt_array(self::$ch, array(
CURLOPT_URL => 'http://www.kuaidi100.com/query'.$query,
CURLOPT_POST => 0
));
$response = curl_exec(self::$ch);
//$response = json_decode($response, true);
return $response;
}
public function quick_query() {
$this->query_com();
return $this->query_num();
}
}
function random_float($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
?>