...
|
...
|
@@ -13,6 +13,7 @@ namespace Api; |
|
|
|
|
|
class Yohobuy
|
|
|
{
|
|
|
|
|
|
const API_URL = 'http://api2.open.yohobuy.com/';
|
|
|
const SERVICE_URL = 'http://service.api.yohobuy.com/';
|
|
|
|
...
|
...
|
@@ -26,11 +27,11 @@ class Yohobuy |
|
|
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
|
|
|
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
|
|
|
);
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 取得当前的客户端类型
|
|
|
*/
|
|
|
public static function clientType()
|
|
|
public static function clientType()
|
|
|
{
|
|
|
// 苹果设备
|
|
|
if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
|
...
|
...
|
@@ -45,7 +46,7 @@ class Yohobuy |
|
|
return 'android';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 取得公共的参数
|
|
|
*
|
...
|
...
|
@@ -64,7 +65,7 @@ class Yohobuy |
|
|
);
|
|
|
return $param;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 构建URL
|
|
|
*
|
...
|
...
|
@@ -74,6 +75,10 @@ class Yohobuy |
|
|
*/
|
|
|
public static function httpBuildQuery($url, $data)
|
|
|
{
|
|
|
// 销毁私钥参数
|
|
|
if (isset($data['private_key'])) {
|
|
|
unset($data['private_key']);
|
|
|
}
|
|
|
if (strstr($url, '?') !== false) {
|
|
|
$url .= '&' . http_build_query($data, null, '&');
|
|
|
} else {
|
...
|
...
|
@@ -81,7 +86,7 @@ class Yohobuy |
|
|
}
|
|
|
return $url;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* get方式调用接口
|
|
|
*
|
...
|
...
|
@@ -93,13 +98,14 @@ class Yohobuy |
|
|
*/
|
|
|
public static function get($url, $data = array(), $returnJson = false, $timeout = 5)
|
|
|
{
|
|
|
// 销毁私钥参数
|
|
|
if (isset($data['private_key'])) {
|
|
|
unset($data['private_key']);
|
|
|
}
|
|
|
if (!empty($data)) {
|
|
|
$url = self::httpBuildQuery($url, $data);
|
|
|
}
|
|
|
|
|
|
|
|
|
$ch = curl_init($url);
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
...
|
...
|
@@ -128,11 +134,12 @@ class Yohobuy |
|
|
public static function post($url, $data = array(), $returnJson = false, $timeout = 5, $header = array(), $cookie = array())
|
|
|
{
|
|
|
$ch = curl_init($url);
|
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
if (!empty($header)) {
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
} else {
|
|
|
}
|
|
|
else {
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
}
|
|
|
|
...
|
...
|
@@ -146,6 +153,7 @@ class Yohobuy |
|
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
// 销毁私钥参数
|
|
|
if (isset($data['private_key'])) {
|
|
|
unset($data['private_key']);
|
|
|
}
|
...
|
...
|
@@ -158,7 +166,7 @@ class Yohobuy |
|
|
}
|
|
|
curl_close($ch);
|
|
|
$data = array();
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
|
...
|
...
|
@@ -173,14 +181,15 @@ class Yohobuy |
|
|
{
|
|
|
$result = array();
|
|
|
$response = array();
|
|
|
$running = null;
|
|
|
$running = 0;
|
|
|
$data = '';
|
|
|
$error = '';
|
|
|
$defaultOptions = array(
|
|
|
CURLOPT_HEADER => 0,
|
|
|
CURLOPT_RETURNTRANSFER => 1,
|
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
|
|
CURLOPT_TIMEOUT => 5,
|
|
|
CURLOPT_CONNECTTIMEOUT => 3,
|
|
|
CURLOPT_TIMEOUT => 3,
|
|
|
CURLOPT_NOSIGNAL => 1, //忽略所有的curl传递给php的信号,减少并发crash
|
|
|
);
|
|
|
$mh = curl_multi_init();
|
|
|
$ch = array();
|
...
|
...
|
@@ -202,17 +211,20 @@ class Yohobuy |
|
|
|
|
|
// 调用API接口
|
|
|
do {
|
|
|
$status = curl_multi_exec($mh, $running);
|
|
|
}
|
|
|
while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
while ($running && $status == CURLM_OK) {
|
|
|
if (curl_multi_select($mh, 0.5) != -1) {
|
|
|
do {
|
|
|
$status = curl_multi_exec($mh, $running);
|
|
|
}
|
|
|
while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
do {
|
|
|
$status = curl_multi_exec($mh, $running);
|
|
|
}
|
|
|
while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
|
|
|
if ($status != CURLM_OK) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if ($running > 0) {
|
|
|
curl_multi_select($mh, 0.5);
|
|
|
}
|
|
|
}
|
|
|
while ($running);
|
|
|
|
|
|
// 获取API接口响应的结果
|
|
|
foreach ($urlList as $name => $api) {
|
...
|
...
|
@@ -236,26 +248,49 @@ class Yohobuy |
|
|
curl_close($ch[$name]);
|
|
|
}
|
|
|
curl_multi_close($mh);
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* rpc调用远程服务(YAR)
|
|
|
*
|
|
|
* @see http://php.net/manual/zh/yar-client.setopt.php
|
|
|
* @return array
|
|
|
*/
|
|
|
public static function yarClient($uri, $method, $data = array(), $timeout = 3000)
|
|
|
public static function yarClient($uri, $method, $parameters = array(), $timeout = 3000)
|
|
|
{
|
|
|
$client = new \Yar_Client($uri);
|
|
|
$client->SetOpt(YAR_OPT_PACKAGER, 'php');
|
|
|
$client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
|
|
|
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
|
|
|
|
|
|
$result = $client->$method($data);
|
|
|
|
|
|
$result = $client->$method($parameters);
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 并行(异步)调用远程服务
|
|
|
*
|
|
|
* @see http://php.net/manual/zh/class.yar-concurrent-client.php
|
|
|
* @param string $uri
|
|
|
* @param string $method
|
|
|
* @param array $parameter
|
|
|
* @param callable $callback
|
|
|
* @param int $timeout
|
|
|
* @return void
|
|
|
*/
|
|
|
public static function yarConcurrentCall($uri, $method, $parameters, $callback, $timeout = 3000)
|
|
|
{
|
|
|
\Yar_Concurrent_Client::call($uri, $method, array($parameters), $callback, null, array(
|
|
|
YAR_OPT_PACKAGER => 'php',
|
|
|
YAR_OPT_TIMEOUT => $timeout,
|
|
|
YAR_OPT_CONNECT_TIMEOUT => $timeout
|
|
|
));
|
|
|
}
|
|
|
public static function yarConcurrentLoop($callback = null)
|
|
|
{
|
|
|
\Yar_Concurrent_Client::loop($callback);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|