Authored by hf

do add api library to call yohobuy app api

<?php
namespace Api;
/**
* 签名相关的操作类
*
* @name Sign
* @package Library/Api
* @copyright yoho.inc
* @version 1.0 (2015-9-30 16:24:36)
* @author fei.hong <fei.hong@yoho.cn>
*/
class Sign
{
/**
* 排序参数
*
* @param array $package 需要签名的数据
* @return array
*/
public static function packageSort(array $package)
{
ksort($package);
reset($package);
return $package;
}
/**
* 生成签名
*
* @param array $package 需要签名的数据
* @return string
*/
public static function makeSign(array $package)
{
$packageList = array();
foreach ($package as $key => $val)
{
$packageList[] = trim($key . '=' . $val);
}
return strtolower(md5(implode('&', $packageList)));
}
/**
* 获取签名
*
* @param array $package 需要签名的数据
* @return string
*/
public static function getSign(array $package)
{
$package = self::packageSort($package);
return self::makeSign($package);
}
/**
* 验证签名
*
* @param array $package 需要签名的数据
* @param string $sign 签名信息
* @return bool (true:签名正确, false:签名错误)
*/
public static function verifySign($package, $sign)
{
$mySign = self::getSign($package);
if ($mySign === $sign) {
return true;
} else {
return false;
}
}
}
... ...
<?php
/**
* 有货相关接口类
*
* @name Yohobuy
* @package library/Api
* @copyright yoho.inc
* @version 1.0 (2015-9-30 16:42:51)
* @author fei.hong <fei.hong@yoho.cn>
*/
namespace Api;
//use Api\Sign;
class Yohobuy
{
const API_URL = 'http://api.open.yohobuy.com/';
const SERVICE_URL = 'http://service.api.yohobuy.com/';
/**
* 私钥列表
*
* @var array
*/
public static $privateKeyList = array(
'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
);
/**
* 取得公共的参数
*
* @return array
*/
public static function param()
{
$param = array(
'client_type' => '',
'app_version' => '',
'os_version' => '',
'screen_size' => '',
'v' => '',
);
return $param;
}
/**
* get方式调用接口
*
* @param string $url 接口URL
* @param int $timeout 超时时间
* @return mixed
*/
public static function get($url, $timeout = 5)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (!empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
return $result;
}
/**
* post提交数据
*
* @param string $url 接口URL
* @param array $data
* @param int $timeout
* @param array $header
* @param array $cookie
* @return mixed
*/
public static function post($url, $data = array(), $timeout = 5, $header = array(), $cookie = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$result = curl_exec($ch);
if (!empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
return $result;
}
/**
* 批量调用接口
*
* @param array $urlList 接口列表
* @param array $options CURL设置项
* @return array
*/
public function loop($urlList = array(), $options = array())
{
$result = array();
$response = array();
$running = null;
$data = '';
$error = '';
$defaultOptions = array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
);
$mh = curl_multi_init();
$ch = array();
// 应用CURL配置
if (empty($options)) {
$options = $defaultOptions;
} else {
$options = array_merge($defaultOptions, $options);
}
// 添加子链接句柄
foreach ($urlList as $name => $api) {
$ch[$name] = curl_init($api);
curl_setopt_array($ch[$name], $options);
curl_multi_add_handle($mh, $ch[$name]);
$result[$name] = array();
}
// 调用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);
}
}
// 获取API接口响应的结果
foreach ($urlList as $name => $api) {
$error = curl_error($ch[$name]);
if ($error != '') {
continue;
}
$data = curl_multi_getcontent($ch[$name]);
if (!$data) {
continue;
}
$response = json_decode($data, true);
if (empty($response['data'])) {
continue;
}
$result[$name] = $response['data'];
curl_multi_remove_handle($mh, $ch[$name]);
curl_close($ch[$name]);
}
curl_multi_close($mh);
return $data;
}
}
... ...
... ... @@ -29,4 +29,20 @@ server
include fastcgi_params;
}
}
server
{
listen 80;
server_name static.dev.yohobuy.com;
#access_log /nginx/logs/access_test_yoho_cn.log combined;
#error_log /nginx/logs/error_test_yoho_cn.log warn;
root D:/workspace/yohobuy.git.dev.yoho.cn/static;
location / {
expires 1h;
}
}
\ No newline at end of file
... ...
... ... @@ -19,7 +19,7 @@ application.dispatcher.defaultController = "index"
application.dispatcher.defaultAction = "index"
;;初始化命名空间
application.namespaces = "Action,Configs,Plugin"
application.namespaces = "Action,Api,Plugin"
;;使用composer
composer.autoload = 0
... ...
... ... @@ -19,7 +19,7 @@ application.dispatcher.defaultController = "index"
application.dispatcher.defaultAction = "index"
;;初始化命名空间
application.namespaces = "Action,Configs,Plugin"
application.namespaces = "Action,Api,Plugin"
;;使用composer
composer.autoload = 0
... ...
... ... @@ -19,7 +19,7 @@ application.dispatcher.defaultController = "index"
application.dispatcher.defaultAction = "index"
;;初始化命名空间
application.namespaces = "Action,Configs,Plugin"
application.namespaces = "Action,Api,Plugin"
;;使用composer
composer.autoload = 0
... ...