轉自:http://wangblog.org/2011/06/api2php.htmlphp
config.phphtml
<?php
header('Content-type: text/html; charset=utf-8');
$aSecret = 'xxxxxx';
$aKey = 'xxxxxx';
$aLogin = 'http://container.open.taobao.com/container?appkey=[key]&encode=utf-8';
$aUrl = 'http://gw.api.taobao.com/router/rest';
?>
login.phpjson
<?php
include('config.php');
if(empty($_COOKIE['as'])){
$aSession = '';
echo('<a href="'.str_replace('[key]', $aKey, $aLogin).'">Login</a>');
}else{
$aSession = $_COOKIE['as'];
echo('loged');
}
?>
api
callback.php數組
<?php
include('config.php');
if(!empty($_REQUEST['top_appkey'])
&& !empty($_REQUEST['top_parameters'])
&& !empty($_REQUEST['top_session'])
&& !empty($_REQUEST['top_sign'])
&& $_REQUEST['top_sign']==base64_encode(md5($_REQUEST['top_appkey'].$_REQUEST['top_parameters'].$_REQUEST['top_session'].$aSecret, true))){
$aSession = $_REQUEST['top_session'];
setcookie('as', $_REQUEST['top_session'], 0, '/');
$aParameters=array();
parse_str(base64_decode($_REQUEST['top_parameters']), $aParameters); //parse_str() 函數把查詢字符串解析到變量中。
echo('<pre>');
print_r($aParameters);
echo('</pre>');
}else{
echo('<pre>');
print_r($_REQUEST);
echo('</pre>');
exit;
}
function get_api($msg){
$d = @json_decode($msg);
return $d;
}
function getCurl($url, $data=null, $header=null){
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
if(!empty($data)){
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
}
if(!empty($header)){
curl_setopt($c, CURLOPT_HTTPHEADER, $header);
}
$d['data'] = curl_exec($c);
$d['header'] = curl_getinfo($c);
curl_close($c);
return $d;
}
$topurl=$aUrl;
$tu['method']='taobao.trades.sold.get';
$tu['session']=$aSession;
$tu['timestamp']=date('Y-m-d H:i:s');
$tu['format']='json';
$tu['app_key']=$aKey;
$tu['v']='2.0';
$tu['sign_method']='md5';
$tu['fields']='orders';
$tu['start_created'] = date('Y-m-d H:i:s', strtotime('-30 day'));
ksort($tu); //ksort() 函數按照鍵名對數組排序,爲數組值保留原來的鍵。
$sign=$aSecret;
foreach($tu as $k=>$v){
$sign.=$k.$v;
}
$sign.=$aSecret;
$tu['sign'] =strtoupper(md5($sign));
$url=$topurl.'?'.http_build_query($tu, '', '&'); //生成 url-encoded 以後的請求字符串
$d=getCurl($url);
echo('<pre>');
print_r(get_api($d['data']));
echo('</pre>');
?>
cookie