淘宝api的封装

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


 $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;  
    }  
}  
?>  


下面是调用的方法:

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); 

你可能感兴趣的:(PHP)