Authored by hf

do modify call yohobuy api add yar corncurrent loop

... ... @@ -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();
... ... @@ -201,17 +210,21 @@ 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 {
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) {
... ... @@ -235,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);
}
}
... ...
<?php
use Action\AbstractAction;
namespace LibModels\Wap\Guang;
use Api\Sign;
use Api\Yohobuy;
/**
* 明星品牌和原创品牌
* 明星品牌和原创品牌相关的数据模型
*
* @name PlusstarData
* @package Library/LibModels/Wap/Guang
* @copyright yoho.inc
* @version 1.0 (2015-10-9 10:22:10)
* @author fei.hong <fei.hong@yoho.cn>
*/
class PlusstarController extends AbstractAction
class PlusstarData
{
/**
* 品牌列表页
* 品牌列表
*
* @param string $gender "1,3"表示男, "2,3"表示女
* @param string $channel 1表示男, 2表示女
* @return array(
* "star" => array(明星品牌列表数据),
* "original" => array(原创品牌列表数据)
* )
*/
public function listAction()
public static function brandList($gender, $channel)
{
$this->_view->assign('title', 'YOHO!有货');
$this->_view->display('list', array('test' => 'hello world'));
// 存放接口列表
$urlList = array();
// 接口调用的URL
$url = Yohobuy::SERVICE_URL . 'guang/api/v1/plustar/getlist';
// 公共的参数
$param = Yohobuy::param();
$param['gender'] = $gender;
$param['is_recommend'] = '0';
$param['yh_channel'] = $channel;
// 构建明星品牌参数及调用接口的URL
$star = $param;
$star['brand_type'] = '2';
$star['client_secret'] = Sign::getSign($star);
$urlList['star'] = Yohobuy::httpBuildQuery($url, $star);
// 构建原创品牌参数及调用接口的URL
$original = $param;
$original['brand_type'] = '3';
$original['client_secret'] = Sign::getSign($original);
$urlList['original'] = Yohobuy::httpBuildQuery($url, $original);
return Yohobuy::getMulti($urlList);
}
/**
* 品牌介绍
* 品牌介绍
*/
public function detailAction()
public static function brandInfo($id)
{
$result = array();
Yohobuy::yarConcurrentCall('http://service.api.yohobuy.com/guang/service/v1/plustar/', 'getBrandInfo', array('id' => 289), function($retval, $callinfo) use(&$result) {
$result[ $callinfo['method'] ] = empty($retval['data']) ? array() : $retval['data'];
});
Yohobuy::yarConcurrentCall('http://service.api.yohobuy.com/guang/service/v1/plustar/', 'getList', array('id' => 289), function($retval, $callinfo) use(&$result) {
$result[ $callinfo['method'] ] = empty($retval['data']) ? array() : $retval['data'];
});
Yohobuy::yarConcurrentLoop();
return $result;
}
}
\ No newline at end of file
}
... ...
<?php
namespace LibModels\wap\Channel;
namespace LibModels\Wap\Product;
use Api\Yohobuy;
use Api\Sign;
... ... @@ -60,7 +60,7 @@ class NewsaleData
$urlList['week'] = Yohobuy::httpBuildQuery(Yohobuy::API_URL,$param);
$param['dayLimit'] = 3;
$urlList['sale'] = Yohobuy::httpBuildQuery(Yohobuy::API_URL,$param);
// var_dump($urlList);exit;
//var_dump($urlList);exit;
return Yohobuy::getMulti($urlList);
}
... ...
<?php
use Action\AbstractAction;
use Api\Yohobuy;
use LibModels\Wap\Guang\PlusstarData;
/**
* 频道选择
... ... @@ -12,6 +12,7 @@ class IndexController extends AbstractAction
$this->_view->assign('title', 'YOHO!有货');
$this->_view->display('index', array('test' => 'hello world'));
//$test = Yohobuy::yarClient('http://service.api.yohobuy.com/guang/service/v1/plustar/', 'getBrandInfo', array('id' => 289));
$test = PlusstarData::brandInfo();
var_dump($test);
}
}
\ No newline at end of file
... ...
<?php
use Action\AbstractAction;
use LibModels\wap\Channel\NewsaleData as Newsale;
use LibModels\wap\Product\NewsaleData as Newsale;
use Plugin\Helpers;
/**
* 新品到着
... ...