<?php
class Alibaba {
public $appKey;
public $secKey;
public $syncAPIClient;
public $serverHost = "https://gw.open.1688.com/openapi/";
public $webSite = '1688';
static public $access_token = '';
public $debug = false;
/**
* 查询
* @param $Param
*/
function Api($api='',$Param = array()) {
$apiInfo = "param2/1/{$api}/" . $this->appKey; //api
$code_arr = array('access_token' => $this->access_token);
foreach ($Param as $k => $v) {
$code_arr[$k] = $v;
}
$code_arr['_aop_signature'] = $this->get_aop_signature($code_arr, $apiInfo);
$Alibabahelper = new Alibabahelper();
$Return = $Alibabahelper->curl_https_post($this->serverHost . $apiInfo, $code_arr);
$Return = json_decode($Return, true);
return $Return;
}
/**
* 獲取簽名
* Enter description here ...
* @param unknown_type $code_arr
* @param unknown_type $apiInfo
*/
public function get_aop_signature($code_arr = array(), $apiInfo) {
$aliParams = array();
foreach ($code_arr as $key => $val) {
$aliParams[] = $key . $val;
}
sort($aliParams);
$sign_str = join('', $aliParams);
$sign_str = $apiInfo . $sign_str;
$code_sign = strtoupper(bin2hex(hash_hmac("sha1", $sign_str, $this->secKey, true)));
return $code_sign;
}
/**
*
* Enter description here ...
* @param unknown_type $redirectUrl
*/
public function getAPIClient($redirectUrl) {
$get_code_url = "https://auth.1688.com/oauth/authorize?client_id={$this->appKey}&site=1688&redirect_uri={$redirectUrl}";
return $get_code_url;
}
/**
*
* Enter description here ...
* @param unknown_type $code
* @param unknown_type $redirectUrl
*/
public function getToken($code, $redirectUrl) {
$url = $this->serverHost;
$url.= "http/1/system.oauth2/getToken/{$this->appKey}?";
$url.= "grant_type=authorization_code";
$url.= "&need_refresh_token=true";
$url.= "&client_id={$this->appKey}";
$url.= "&client_secret={$this->secKey}";
$url.= "&redirect_uri={$redirectUrl}";
$url.= "&code={$code}";
$Alibabahelper = new Alibabahelper();
$s = $Alibabahelper->curl_https_get($url);
$s = json_decode($s, true);
return $s;
}
/**
*
* Enter description here ...
* @param unknown_type $refreshToken
*/
public function refreshToken($refreshToken) {
$url = "https://gw.api.alibaba.com/openapi/param2/1/system.oauth2/getToken/{$this->appKey}?";
$url.= "grant_type=refresh_token&client_id={$this->appKey}&";
$url.= "client_secret={$this->secKey}&refresh_token={$refreshToken}";
$Alibabahelper = new Alibabahelper();
$s = $Alibabahelper->curl_https_get($url);
$s = json_decode($s, true);
return $s;
}
/**
*
* Enter description here ...
* @param $msg
*/
function debug($msg) {
if ($this->debug) {
print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;">';
echo "<pre>";
print_r($msg);
echo "</pre>";
print '</div>';
}
}
}
class Alibabahelper {
function __construct() {
}
private function curl_get_contents($url, $data = array(), $https = false) {
$results['error'] = '';
$results['status'] = 0;
$results['data'] = array();
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$curl = curl_init(); // 启动一个CURL会话
if (!empty($data) && is_array($data)) {
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
}
if ($https) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
}
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
$results['data'] = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
$results['error'] = curl_error($curl); //捕抓异常
}
curl_close($curl); // 关闭CURL会话
return $results['data']; // 返回数据
}
public function curl_https_post($url, $data) {
return $this->curl_get_contents($url, $data, true);
}
public function curl_https_get($url) {
return $this->curl_get_contents($url, array(), true);
}
}
?>
//使用例子
$Alibaba=new Alibaba();
$Alibaba->appKey='****';
$Alibaba->secKey='****';
$Alibaba->access_token ='***'
$result=$Alibaba->Api('com.alibaba.account/alibaba.account.basic');
print_r($result);