淘宝api的封装

这是全部写在一起的,你们可以根据需求,拆分成类,就可以直接用了


<?php  
/** 
 * Filename: TopApi.php 
 * Created:  2010-12-20 
 * Author:   blog.csdn.net/jakieyoung 
 * License:  free 
 */  
if(!defined('ENVIRONMENT')) define('ENVIRONMENT', 'devel');  
if (ENVIRONMENT == 'devel') {  
    define('AppKey'     , 'test');  
    define('AppSecret'  , 'test');  
    define('Gateway'    , 'http://gw.api.tbsandbox.com/router/rest');  
} else if (ENVIRONMENT == 'product') {  
    define('AppKey'     , '12345678');  
    define('AppSecret'  , '------------------------------------');  
    define('Gateway'    , 'http://gw.api.taobao.com/router/rest');  
} else {  
    die("ENVIRONMENT is neither devel nor product");  
}  
define('Format',     'json');  
define('SignMethod', 'md5');  
define('APIVersion', '2.0');  
define('SDKVersion', 'top_api_php_1.0');  
function sign($params) {  
    $items = array();  
    foreach($params as $key => $value) $items[$key] = $value;  
    ksort($items);  
    $s = AppSecret;  
    foreach($items as $key => $value) {  
        $s .= "$key$value";  
    }  
    $s .= AppSecret;  
    return strtoupper(md5($s));  
}  
function decode_top_parameters($top_parameters) {  
    $params = array();  
    $param_array = explode('&', base64decode($top_parameters));  
    foreach($param_array as $p) {  
        list($key, $value) = explode('=', $p);  
        $params[$key] = $value;  
    }  
    return $params;  
}  
class TopRequest {  
    private $method_name;  
    private $api_params = array();  
    function TopRequest($method_name) {  
        $this->method_name = $method_name;  
    }  
    function set_param($param_name, $param_vaule) {  
        $this->api_params[$param_name] = $param_vaule;  
    }  
    function get_api_params() {  
        return $this->api_params;  
    }  
    function get_method_name() {  
        return $this->method_name;  
    }  
    function execute($session = '') {  
        $client = new TopClient;  
        return $client->execute($this, $session);  
    }  
}  
class TopClient {  
    function execute($request, $session = '') {  
        $sys_params = array(  
                'app_key' => AppKey,  
                'format'  => Format,  
                'v'       => APIVersion,  
                'sign_method' => SignMethod,  
                'partner_id'  => SDKVersion  
        );  
        $api_params = $request->get_api_params();  
        $sys_params['method'] = $request->get_method_name();  
        $sys_params['timestamp'] = date("Y-m-d H:i:s");  
        if($session != '') {  
            $sys_params['session'] = $session;  
        }  
        $sys_params['sign'] = sign(array_merge($sys_params, $api_params));  
        $param_string = '';  
        foreach($sys_params as $p => $v) {  
            $param_string .= "$p=" . urlencode($v) . "&";  
        }  
        $url = Gateway . '?' . substr($param_string, 0, -1);  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);  
        curl_setopt($ch, CURLOPT_POST, true);  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $api_params);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        $postResult = curl_exec($ch);  
        if (curl_errno($ch)){  
            throw new Exception(curl_error($ch), 0);  
        } else {  
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
            if (200 !== $httpStatusCode) {  
                throw new Exception($postResult, $httpStatusCode);  
            }  
        }  
        curl_close($ch);  
        $obj = json_decode($postResult);  
        foreach($obj as $k => $v) {  
            $obj = $v;  
        }  
        return $obj;  
    }  
}  
?>  


下面是调用的方法:

<?php  
include_once('TopApi.php');  
$top_session = "24523150b447abcb617cc1d7b58ce71ad7230";  
$iid = 2304232983;  
$new_image_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'nvzhuang.jpg';  
echo $new_image_path;  
$req = new TopRequest('taobao.item.img.upload');  
$req->set_param('iid', $iid);  
$req->set_param('image', '@' . $new_image_path); //上传文件,在文件路径前加上AT符号  
$req->set_param('is_major', 'true');  
$result = $req->execute($top_session); // 对于不需要session的api,则可以不用session参数  
var_dump($result); 

猜你喜欢

转载自blog.csdn.net/chris_zqw/article/details/80354588