Simple record of steps to create Taobao open platform application

Source: Internet
Author: User
Tags curl md5 oauth urlencode
Note the steps of Taobao open platform application development.
1, register as a developer, create an application,
Refer to the links below to select an application
http://open.taobao.com/doc/detail.htm?id=101710
When creating an application, there is a callback address URL,
The callback address is used to receive the top (open platform) return authorization-related data.
Here's how your app uses the top API: Tests are done in so-called sandbox mode before they are online.
1, take the Web application as an example, users visit your web, your Web application redirect users to Taobao login authentication, such as the following
https://oauth.tbsandbox.com/authorize?response_type=code&client_id=1021738064&redirect_uri=http%3A%2F% 2fdev2dev.sinaapp.com%2ftaobao%2fcallback.php&from_site=fuwu
2, user login with Taobao account, Redirec user to callback URL, is your application of a URL
3, an authorization code will be passed as a parameter to the callback URL, your application uses this authorization code to post the way to access Taobao's
Https://oauth.tbsandbox.com/token Get Token
4, after getting token can call Taobao API.
For more information on authentication, refer to the links below
http://open.taobao.com/doc/detail.htm?spm=0.0.0.0.CSGRVZ&id=118
Note the point:
1, in the test when using the sandbox environment Appkey and Appsecret, Taobao end of the certification of the URL is also the tbsandbox of the sandbox environment.
2, the login account must also be in the sandbox environment, Http://www.tbsandbox.com/doc/index.html#taobao_acount
3, Taobao offers a sample call http://open.taobao.com/doc/detail.htm?spm=0.0.0.0.mtTHeu&id=131 ' Session_key ' => $sessionkey, Should be changed to ' session ' => $sessionkey,
Here is the code for the callback page:
<?php function Curl ($url, $postFields = null) {$ch = Curl_init ();
		curl_setopt ($ch, Curlopt_url, $url);
		curl_setopt ($ch, Curlopt_failonerror, false);

		curl_setopt ($ch, Curlopt_returntransfer, true); if (strlen ($url) > 5 && strtolower (substr ($url, 0,5)) = = "https") {curl_setopt ($ch, Curlopt_ssl_verifypeer,
			FALSE);
		curl_setopt ($ch, Curlopt_ssl_verifyhost, false);
			} if (Is_array ($postFields) && 0 < count ($postFields)) {$postBodyString = "";
			$postMultipart = false;  foreach ($postFields as $k => $v) {if ("@"!= substr ($v, 0, 1))//Determine if file upload {$postBodyString. = "$k =". UrlEncode ($v). 
				"&";
				else//file upload with Multipart/form-data, otherwise use www-form-urlencoded {$postMultipart = true;
			} unset ($k, $v);
			curl_setopt ($ch, Curlopt_post, true);
			if ($postMultipart) {curl_setopt ($ch, Curlopt_postfields, $postFields); else {curl_setopt ($ch, Curlopt_postfields, substr ($postBodySTRING,0,-1));
		
		}} $reponse = Curl_exec ($ch);
		if (Curl_errno ($ch)) {throw new Exception (Curl_error ($ch), 0);
			else {$httpStatusCode = Curl_getinfo ($ch, Curlinfo_http_code);
			if (!== $httpStatusCode) {throw new Exception ($reponse, $httpStatusCode);
		} curl_close ($ch);
	return $reponse;
Header ("Content-type:text/html;charset=utf-8");
Require_once ' util.php ';
$appKey = ' Appkey of sandbox environment ';
$appSecret = ' Appsecret of sandbox environment ';
$sessionkey = ' Test ';
 $code = $_get["code"];
$code = $_get["top_session"];//prod enc
$PARAMARR = Array (' Code ' => $code, ' grant_type ' => ' authorization_code ', ' client_id ' => $appKey, ' Client_secret ' => $appSecret, ' Redirect_uri ' => ' http://dev2dev.sinaapp.com/taobao/test.php ');//parameter array $url = ' https:// Oauth.tbsandbox.com/token ';
$url = ' Https://oauth.taobao.com/token '; Prod env
Echo Curl ($url, $PARAMARR); Display return information echo "$code";? >
The following is the application call Taobao API code: Using the callback page to get the session tokentest.php
<?php header ("Content-type:text/html;charset=utf-8");
Require_once ' util.php ';
$appKey = ' Appkey of the Sand box ';
$appSecret = ' Appsecret of the Sand box '; $sessionkey = ' 6202a275b902c321c324db810764ecbae6ace39da3f702f2074082787 ';//callback page get code and then get the session token//parameter array $ Paramarr = Array (' App_key ' => $appKey, ' Session ' => $sessionkey, ' method ' => ' Taobao.user.seller. Get ', ' format ' => ' json ', ' V ' => ' 2.0 ', ' Sign_method ' => ' MD5 ', ' timestamp ' => date (' y-m-d H : I:s '), ' Fields ' => ' User_id,nick,sex,seller_credit,type,has_more_pic,item_img_num,item_img_size,prop_img_num, Prop_img_size,auto_repost,promoted_type,status,alipay_bind,consumer_protection,avatar,liangpin,sign_food_ Seller_promise,has_shop,is_lightning_consignment,has_sub_stock,is_golden_seller,vip_info,magazine_subscribe,

Vertical_market,online_gaming ');
Generate Signature $sign = Createsign ($PARAMARR);
Tissue parameter $strParam = Createstrparam ($PARAMARR);
$strParam. = ' sign= '. $sign; Access Service $url = ' Http://gw.api.tbsAndbox.com/router/rest? '. $strParam; Sandbox environment Call address//$url = ' http://gw.api.taobao.com/router/rest? '. $strParam;
Prod call address $result = file_get_contents ($url);
$result = Json_decode ($result);
echo "JSON's structure is:";
Print_r ($result);
echo "<br>";
echo "User name is:". $result->user_get_response->user->nick;
echo "<br>"; echo "Buyer credit rating:". $result->user_get_response->user->buyer_credit->level;?>

util.php

<?php
//Signature functions function
createsign ($PARAMARR) {
     global $appSecret;
     $sign = $appSecret;
     Ksort ($PARAMARR);
     foreach ($paramArr as $key => $val) {
         if ($key!= ' && $val!= ') {
             $sign. = $key. $val;
         }
     }
     $sign. = $appSecret;
     $sign = Strtoupper (MD5 ($sign));
     return $sign;
}

Group parameter function
Createstrparam ($PARAMARR) {
     $strParam = ';
     foreach ($paramArr as $key => $val) {
     if ($key!= ' && $val!= ') {
             $strParam. = $key. ' = '. UrlEncode ($val). ' & ';
         }
     }
     return $strParam;
>
If you want to use formal environmental testing, you need to change the appkey,appsecret into a formal environment.
The parameter of the callback URL that is passed to the application after authentication is $code = $_get["top_session"];//prod enc
Taobao user authentication URL should also be replaced by the formal environment
Http://container.api.taobao.com/container?appkey={appkey}
Taobao token certification URL should also be replaced by the formal environment
$url = ' Https://oauth.taobao.com/token '; Prod env
The API call URL is replaced by the following

$url = ' http://gw.api.taobao.com/router/rest? '. $strParam; Prod call address

User Authentication Entry

<div id= "Container" >
<a href= "https://oauth.tbsandbox.com/authorize?response_type=code&client_id=xxxx&redirect_uri=http% 3a%2f%2fdev2dev.sinaapp.com%2ftaobao%2fcallback.php&from_site=fuwu ">login sandbox</a>
<br>
<a href= "http://container.api.taobao.com/container?appkey=yyy" >login prod</a>
</div>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.