热搜:NVER node 开发 php

关于淘宝API返回XML简单对象提取为数组的问题?

2024-08-13 19:35:01
关于淘宝API返回XML简单对象提取为数组的问题?

一、通过淘宝API获得某个商品信息,得到如下XML对象
SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [auction_point] => 5 [detail_url] => http://item.taobao.com/item.htm?id=42234097497&spm=2014.23062087.0.0 [item_imgs] => SimpleXMLElement Object ( [@attributes] => Array ( [list] => true ) [item_img] => Array ( [0] => SimpleXMLElement Object ( [id] => 0 [position] => 0 [url] => http://img01.taobaocdn.com/bao/uploaded/i1/TB1gY_gGpXXXXX8apXXXXXXXXXX_!!0-item_pic.jpg ) [1] => SimpleXMLElement Object ( [id] => 17030079984 [position] => 1 [url] => http://img01.taobaocdn.com/bao/uploaded/i1/TB1.lKwGXXXXXXyapXXXXXXXXXX_!!0-item_pic.jpg ) [2] => SimpleXMLElement Object ( [id] => 17030079985 [position] => 2 [url] => http://img03.taobaocdn.com/bao/uploaded/i3/1703225540/TB25UT7aVXXXXXUXpXXXXXXXXXX_!!1703225540.jpg ) [3] => SimpleXMLElement Object ( [id] => 17030079986 [position] => 3 [url] => http://img02.taobaocdn.com/bao/uploaded/i2/1703225540/TB2.3r6aVXXXXX3XpXXXXXXXXXX_!!1703225540.jpg ) [4] => SimpleXMLElement Object ( [id] => 17030079987 [position] => 4 [url] => http://img01.taobaocdn.com/bao/uploaded/i1/1703225540/TB2dV__aVXXXXXlXpXXXXXXXXXX_!!1703225540.jpg ) ) ) [nick] => 雪荞旗舰店 [num_iid] => 42234097497 [price] => 88.00 [title] => 【买1送1】雪荞乌金红滇红茶叶礼云南凤庆特级密香功夫红茶包邮 ) ) Array ( [isError] => 1 [store_type] => SimpleXMLElement Object ( [0] => 5 ) [item_link] => SimpleXMLElement Object ( [0] => http://item.taobao.com/item.htm?id=42234097497&spm=2014.23062087.0.0 ) [img] => SimpleXMLElement Object ( [0] => http://img01.taobaocdn.com/bao/uploaded/i1/TB1gY_gGpXXXXX8apXXXXXXXXXX_!!0-item_pic.jpg ) [item_id] => SimpleXMLElement Object ( [0] => 42234097497 ) [item_title] => SimpleXMLElement Object ( [0] => 【买1送1】雪荞乌金红滇红茶叶礼云南凤庆特级密香功夫红茶包邮 ) [store_id] => SimpleXMLElement Object ( [0] => 雪荞旗舰店 ) [market_price] => SimpleXMLElement Object ( [0] => 88.00 ) ) 

二、赋值给$getdata

三、通过以下方法取值 
$result = array
(
'isError' =>true,
'store_type' =>$getdata->item->auction_point,
'item_link' =>$getdata->item->detail_url,
'img' =>$getdata->item->item_imgs->item_img[0]->url,
'item_id' =>$getdata->item->num_iid,
'item_title' =>$getdata->item->title,
'store_id' =>$getdata->item->nick,
'market_price' =>$getdata->item->price
);
为何得到的值是一个不正确的JSON对象
{"isError":true,"store_type":{"0":"5"},"item_link":{"0":"http:\/\/item.taobao.com\/item.htm?id=42234097497&spm=2014.23062087.0.0"},"img":{"0":"http:\/\/img01.taobaocdn.com\/bao\/uploaded\/i1\/TB1gY_gGpXXXXX8apXXXXXXXXXX_!!0-item_pic.jpg"},"item_id":{"0":"42234097497"},"item_title":{"0":"【买1送1】雪荞乌金红滇红茶叶礼云南凤庆特级密香功夫红茶包邮"},"store_id":{"0":"雪荞旗舰店"},"market_price":{"0":"88.00"}}

四、我想要得到的是
,"store_type":5,,而不是"store_type":{"0":"5"} 请大神帮忙为感!


回复讨论(解决方案)

你把得到的原始字符传贴出来!
你这样的贴法是不行的,看不清楚

淘宝API返回只有两种格式JSON 或者XML,echo 出不来,VAR_DUMP也出不来,只有print_r可以输出

淘宝API返回来的是SimpleXMLElement Object ( [item] => SimpleXMLElement Object ( [auction_point] => 5 [detail_url] => http://item.taobao.com/item.htm?id=42234097497&spm=2014.23062087.0.0 [item_imgs] => SimpleXMLElement Object ( [@attributes] => Array ( [list] => true ) [item_img] => Array ( [0] => SimpleXMLElement Object ( [id] => 0 [position] => 0 [url] => http://img01.taobaocdn.com/bao/uploaded/i1/TB1gY_gGpXXXXX8apXXXXXXXXXX_!!0-item_pic.jpg ) [1] => SimpleXMLElement Object ( [id] => 17030079984 [position] => 1 [url] => http://img01.taobaocdn.com/bao/uploaded/i1/TB1.lKwGXXXXXXyapXXXXXXXXXX_!!0-item_pic.jpg ) [2] => SimpleXMLElement Object ( [id] => 17030079985 [position] => 2 [url] => http://img03.taobaocdn.com/bao/uploaded/i3/1703225540/TB25UT7aVXXXXXUXpXXXXXXXXXX_!!1703225540.jpg ) [3] => SimpleXMLElement Object ( [id] => 17030079986 [position] => 3 [url] => http://img02.taobaocdn.com/bao/uploaded/i2/1703225540/TB2.3r6aVXXXXX3XpXXXXXXXXXX_!!1703225540.jpg ) [4] => SimpleXMLElement Object ( [id] => 17030079987 [position] => 4 [url] => http://img01.taobaocdn.com/bao/uploaded/i1/1703225540/TB2dV__aVXXXXXlXpXXXXXXXXXX_!!1703225540.jpg ) ) ) [nick] => 雪荞旗舰店 [num_iid] => 42234097497 [price] => 88.00 [title] => 【买1送1】雪荞乌金红滇红茶叶礼云南凤庆特级密香功夫红茶包邮 ) ) 

这里是通过$result = array
(
'isError' =>true,
'store_type' =>$getdata->item->auction_point,
'item_link' =>$getdata->item->detail_url,
'img' =>$getdata->item->item_imgs->item_img[0]->url,
'item_id' =>$getdata->item->num_iid,
'item_title' =>$getdata->item->title,
'store_id' =>$getdata->item->nick,
'market_price' =>$getdata->item->price
);取值后得到的结果 
 Array ( [isError] => 1 [store_type] => SimpleXMLElement Object ( [0] => 5 ) [item_link] => SimpleXMLElement Object ( [0] => http://item.taobao.com/item.htm?id=42234097497&spm=2014.23062087.0.0 ) [img] => SimpleXMLElement Object ( [0] => http://img01.taobaocdn.com/bao/uploaded/i1/TB1gY_gGpXXXXX8apXXXXXXXXXX_!!0-item_pic.jpg ) [item_id] => SimpleXMLElement Object ( [0] => 42234097497 ) [item_title] => SimpleXMLElement Object ( [0] => 【买1送1】雪荞乌金红滇红茶叶礼云南凤庆特级密香功夫红茶包邮 ) [store_id] => SimpleXMLElement Object ( [0] => 雪荞旗舰店 ) [market_price] => SimpleXMLElement Object ( [0] => 88.00 ) )

echo $getdata->asXML();

不就得到了吗?

淘宝这个API一天就只让用几次,我今天的用完了,明天我试试这个方法echo $getdata->asXML(); 多谢各位帮助。到时候再请大家帮忙看看。

版主就是版主,非常感谢。