Client.php
2.67 KB
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
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Created by PhpStorm.
* User: liuziyang
* Date: 14-4-25
* Time: 0:46
*/
class Q_Webservice_Yar_Client
{
private $yarClient;
private $requestUrl;
private $url;
public function __construct($url)
{
$this->url = $url;
$this->requestUrl = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
$this->yarClient = new Yar_Client($this->url);
}
/**
* name 可以是: YAR_OPT_PACKAGER, YAR_OPT_PERSISTENT (需要服务端支持keepalive), YAR_OPT_TIMEOUT, YAR_OPT_CONNECT_TIMEOUT
*
* YAR_OPT_CONNECT_TIMEOUT, 1000
*
* YAR_OPT_PACKAGER, "json"
*
* @param $name
* @param $value
*/
public function setOpt($name, $value)
{
$this->yarClient->setOpt($name, $value);
return $this;
}
/**
* 设置调用来源
* @param $referer
* @return $this
*/
public function setReferer($referer)
{
$this->referer = $referer;
return $this;
}
/**
* @param $name
* @param $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
$callTime = microtime(true);
try {
$result = call_user_func_array(array($this->yarClient, $name), $arguments);
$resultTime = microtime(true);
########### 数据包 ###########
$package = array(
'result_type' => 'success',
'function_name' => $name,
'referer' => $this->requestUrl,
'service_url' => $this->url,
'parameter' => $arguments,
'result' => $result,
'call_time' => $callTime,
'result_time' => $resultTime
);
########### 数据包 ###########
QAid_Logs_Record::yar($package);
} catch (Yar_Server_Exception $e) {
$resultTime = microtime(true);
$package = array(
'result_type' => 'error',
'function_name' => $name,
'referer' => $this->requestUrl,
'service_url' => $this->url,
'parameter' => $arguments,
'call_time' => $callTime,
'result_time' => $resultTime,
'exception_data' => array(
'type' => $e->getType(),
'message' => $e->getMessage()
)
);
QAid_Logs_Record::yar($package);
$result = $package;
}
return $result;
}
function call($name, $arguments)
{
$result = call_user_func_array(array($this->yarClient, $name), $arguments);
return $result;
}
}