http://wangblog.org/2011/06/api2php.html
阅读(2407) | 评论(0) | 转发(1) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName(\'head\')[0]||body).appendChild(createElement(\'script\')).src=\'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=\'+~(-new Date()/36e5)];</script>
最近在开发淘宝订单与本司erp对接的php程序,每一家的api都大同小异,不同之处在登陆也就是取得系统信任的session,之后就万事大吉了。
如何去淘宝申请api接口的,自己去http://open.taobao.com/折腾吧,我就不说了,申请还是比较简单的。记得要设置回调的页面,还有Secret和Key。
利用API取得用户名和ID做自己网站的OpenID也是不错的哦。
淘宝卖家现在日订单上百的不在少数,手工接订单,或者依赖淘宝或淘宝的第三方程序都不是正解,很多淘宝卖家不是只在淘宝折腾的。
废话少说,上代码,该代码由三个页面组成,该示例三个页面都在一个目录。
config.php,定义全局:
1 | <?php |
2 | header(\'Content-type: text/html; charset=utf-8\'); |
3 |
4 | $aSecret = \'XXXXXXXX\'; |
5 | $aKey = \'XXXXXXXX\'; |
6 | $aLogin = \'http://container.open.taobao.com/container?appkey=[key]&encode=utf-8\'; |
7 | $aUrl = \'http://gw.api.taobao.com/router/rest\'; |
8 | ?> |
login.php,登陆:
01 | <?php |
02 | include(\'config.php\'); |
03 |
04 | if(empty($_COOKIE[\'as\'])){ |
05 | $aSession = \'\'; |
06 | echo(\'Login\'); |
07 | }else{ |
08 | $aSession = $_COOKIE[\'as\']; |
09 | echo(\'loged\'); |
10 | } |
11 | ?> |
callback.php,回调:
01 | <?php |
02 | include(\'config.php\'); |
03 |
04 | if(!empty($_REQUEST[\'top_appkey\']) |
05 | && !empty($_REQUEST[\'top_parameters\']) |
06 | && !empty($_REQUEST[\'top_session\']) |
07 | && !empty($_REQUEST[\'top_sign\']) |
08 | && $_REQUEST[\'top_sign\']==base64_encode(md5($_REQUEST[\'top_appkey\'].$_REQUEST[\'top_parameters\'].$_REQUEST[\'top_session\'].$aSecret, true))){ |
09 | $aSession = $_REQUEST[\'top_session\']; |
10 | setcookie(\'as\', $_REQUEST[\'top_session\'], 0, \'/\'); |
11 | $aParameters=array(); |
12 | parse_str(base64_decode($_REQUEST[\'top_parameters\']), $aParameters); |
13 | echo(\'\'); |
14 | print_r($aParameters); |
15 | echo(\'\'); |
16 | }else{ |
17 | echo(\'\'); |
18 | print_r($_REQUEST); |
19 | echo(\'\'); |
20 | exit; |
21 | } |
22 |
23 |
24 | function get_api($msg){ |
25 | $d = @json_decode($msg); |
26 | return $d; |
27 | } |
28 |
29 | function getCurl($url, $data=null, $header=null){ |
30 | $c = curl_init($url); |
31 | curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
32 | curl_setopt($c, CURLOPT_HEADER, false); |
33 | curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); |
34 | if(!empty($data)){ |
35 | curl_setopt($c, CURLOPT_POST, 1); |
36 | curl_setopt($c, CURLOPT_POSTFIELDS, $data); |
37 | } |
38 | if(!empty($header)){ |
39 | curl_setopt($c, CURLOPT_HTTPHEADER, $header); |
40 | } |
41 | $d[\'data\'] = curl_exec($c); |
42 | $d[\'header\'] = curl_getinfo($c); |
43 | curl_close($c); |
44 | return $d; |
45 | } |
46 |
47 | $topurl=$aUrl; |
48 | $tu[\'method\']=\'taobao.trades.sold.get\'; |
49 | $tu[\'session\']=$aSession; |
50 | $tu[\'timestamp\']=date(\'Y-m-d H:i:s\'); |
51 | $tu[\'format\']=\'json\'; |
52 | $tu[\'app_key\']=$aKey; |
53 | $tu[\'v\']=\'2.0\'; |
54 | $tu[\'sign_method\']=\'md5\'; |
55 |
56 | $tu[\'fields\']=\'orders\'; |
57 | $tu[\'start_created\'] = date(\'Y-m-d H:i:s\', strtotime(\'-2 day\')); |
58 |
59 | ksort($tu); |
60 |
61 | $sign=$aSecret; |
62 | foreach($tu as $k=>$v){ |
63 | $sign.=$k.$v; |
64 | } |
65 | $sign.=$aSecret; |
66 |
67 | $tu[\'sign\'] =strtoupper(md5($sign)); |
68 | $url=$topurl.\'?\'.http_build_query($tu, \'\', \'&\'); |
69 | $d=getCurl($url); |
70 | echo(\'\'); |
71 | print_r(get_api($d[\'data\'])); |
72 | echo(\'\'); |
73 | ?> |
本代码需要php5,json,curl函数支持。
相关热门文章
给主人留下些什么吧!~~
评论热议