search
HomeBackend DevelopmentPHP Tutorial关于淘宝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(); 多谢各位帮助。到时候再请大家帮忙看看。

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

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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.