<?php
//require './function.php';
function get_md5_string($url,$params){
ksort($params);
$validateString_new ='';
$params_url = array();
foreach($params as $k=>$v){
$params_url[]=$k.'='.$v;
$validateString_new .= mb_convert_encoding(trim($v),'gbk','utf8');
}
$api_key ='bor'; //为商家后台所设置的key 值
$validateString_new .= mb_convert_encoding($api_key,'gbk','utf8');
$url.='?'.implode("&", $params_url);
$validateString =md5($validateString_new);
$url .='&validateString='.$validateString;
return $url;
}
//解析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;
}
//返回商品属性
function curl_getmore($url,$params){
$url= get_md5_string($url,$params);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($ch, CURLOPT_TIMEOUT, 30);//设置cURL允许执行的最长秒数
$data = curl_exec($ch); //执行一个cURL会话
//curl_close($ch); //关闭一个cURL会话
return $data;
curl_close($ch); //关闭一个cURL会话
}
$params = array(
'gShopID'=>'12345',
);
$url = 'http://api.dangdang.com/v2/getDDCategory.php';
$data = curl_getmore($url,$params);
$info = getXmlData($data);
//$total = $info['totalInfo']['pageTotal'];
//print_r($total);
print_r($info);
当当网商家api接口取商品属性php部分代码
最新推荐文章于 2022-08-29 10:31:29 发布