Home  >  Article  >  Backend Development  >  php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法_PHP

php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法_PHP

WBOY
WBOYOriginal
2016-05-30 08:47:351113browse

本文实例讲述了php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法。分享给大家供大家参考。具体实现方法如下:

<&#63;php 
header("Content-Type:text/html;charset=UTF-8"); 
/*本程序功能:根卖家昵称获取卖家店铺sid,还有店铺标题*/
//config 
$appKey = '12345678; //你的密匙 
$appSecret = '123456789'; 
$usernick = 'grayvoice'; //你的用户名 
$salenick= '缺水鱼儿'; //卖家昵称 
//签名函数 
function createSign ($paramArr) { 
global $appSecret; 
$sign = $appSecret; 
ksort($paramArr); 
foreach ($paramArr as $key => $val) { 
if ($key !='' && $val !='') { 
$sign .= $key.$val; 
} 
} 
$sign = strtoupper(md5($sign)); 
return $sign; 
} 
//组参函数 
function createStrParam ($paramArr) { 
$strParam = ''; 
foreach ($paramArr as $key => $val) { 
if ($key != '' && $val !='') { 
$strParam .= $key.'='.urlencode($val).'&'; 
} 
} 
return $strParam; 
} 
//解析xml函数 
function getXmlData ($strXml) { 
$pos = strpos($strXml, 'xml'); 
if ($pos) { 
$xmlCode=simplexml_load_string($strXml,'SimpleXMLElement', LIBXML_NOCDATA); 
$arrayCode=get_object_vars_final($xmlCode); 
return $arrayCode ; 
} else { 
return ''; 
} 
} 
function get_object_vars_final($obj){ 
if(is_object($obj)){ 
$obj=get_object_vars($obj); 
} 
if(is_array($obj)){ 
foreach ($obj as $key=>$value){ 
$obj[$key]=get_object_vars_final($value); 
} 
} 
return $obj; 
} 
//参数数组 
$paramArr = array( 
'app_key' => $appKey, 
'method' => 'taobao.shop.get', 
'format' => 'xml', 
'v' => '1.0', 
'timestamp' => date('Y-m-d H:i:s'), 
'fields' => 'sid,nick,title', /*想要返回的数据的相应参数,sid对应卖家店铺id,nick对应卖家昵称,这里其实不用返回,因为已经知道了嘛。title对应卖家店铺标题,也就是店铺名称*/
'nick' => $salenick //卖家昵称 
); 
//生成签名 
$sign = createSign($paramArr); 
//组织参数 
$strParam = createStrParam($paramArr); 
$strParam .= 'sign='.$sign; 
//访问服务 
$url = 'http://gw.api.taobao.com/router/rest'.$strParam; 
$result = file_get_contents($url); 
$result = getXmlData($result); 
$sid = $result['shop']['sid']; //返回卖家店铺ID 
$nick = $result['shop']['nick']; //返回卖家昵称 
$title = $result['shop']['title']; //返回卖家店铺标题 
&#63;> 
<php echo '卖家店铺ID:'.$sid.'<br>';> <!-- 在页面打印店铺ID --> 
<php echo '卖家昵称:'.$nick.'<br>';> <!-- 在页面打印卖家昵称--> 
<php echo '店铺标题:'.$title.'<br>';> <!-- 在页面打印卖家店铺标题 -->

希望本文所述对大家的php程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn