将字符串类型从curl转换为数组

时间:2013-12-22 01:51:28

标签: php arrays curl

我正在使用这个卷曲代码:

$key='the_key'; 
$url='http://api-product.skimlinks.com/categories?key='.$key.'&format=xml';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec($ch);
var_dump($result);

这给了我以下字符串

string(575317) "<?xml version='1.0' encoding='UTF-8'?>
<skimlinksProductAPI><status>200
</status><message>OK
</message><version>3
</version><categories><category><id>4446
</id><name>Vehicles &amp; Parts &gt; Automotive Monitoring &gt; Radar Detectors &amp; Jammers
</name>
</category><category><id>4458
</id><name>Vehicles &amp; Parts &gt; Automotive Parts &gt; Automotive Batteries &gt; Fuel Cells
</name>
</category><category><id>4412
</id><name>Vehicles &amp; Parts &gt; Automotive Exterior &gt; License Plates
</name>
</category><category><id>3734
</id><name>Sporting Goods &gt; Outdoor Recreation &gt; Camping, Backpacking &amp; Hiking &gt; Camp Furniture
</name>
</category><category><id>4447
</id><name>Vehicles &amp; Parts &gt; Automotive Monitoring &gt; Speedometers
</name>
</category><category><id>4026
</id><name>Sporting Goods &gt; Team Sports &gt; Beach Volleyball &gt; Beach Volleyball Nets
</name>
</category><category><id>3724
</id><name>Sporting Goods &gt; Outdoor Recreation &gt; Archery &gt; Archery Armguards
</name>
</category><category><id>4024
</id><name>Sporting Goods &gt; Team Sports &gt; Beach Volleyball
</name>
</category><category><id>4025
</id><name>Sporting Goods &gt; Team Sports &gt; Beach Volleyball &gt; Beach Volleyball Balls
</name>
</category><category><id>4022
</id><name>Sporting Goods &gt; Team Sports &gt; Basketball &gt; Basketball Hoops
</name>
</category><category><id>4023
</id><name>Sporting Goods &gt; Team Sports &gt; Basketball &gt; Basketballs
</name>
</category><category><id>4020
</id><name>Sporting Goods &gt; Team Sports &gt; Basketball &gt; Basketball Hoop Parts &amp; Accessories &gt; Basketball Poles
</name>
</category><category><id>3725
</id><name>Sporting Goods &gt; Outdoor Recreation &gt; Archery &gt; Archery Targets
</name>
</category><category><id>3798
</id><name>Sporting Goods &gt; Outdoor Recreation &gt; Cycling &gt; Bicycle Parts &gt; Bicycle Forks
</name>
</category><category><id>4028
</id><name>Sporting Goods &gt; Team Sports &gt; Cheerleading &gt; Cheerleading Megaphones
</name>
</category><category><id>4029
</id><name>Sporting Goods &gt; Team Sports &gt; Cheerleading &gt; Pom-Poms
</name>
</category><category><id>4607
</id><name>Vehicles &amp; Parts &gt; Watercraft Parts &amp; Accessories &gt; Watercraft Care &gt; Watercraft Polishes
</name>
</category><category><id>4417
</id><name>Vehicles &amp; Parts &gt; Automotive Exterior &gt; Winch Parts
</name>
</category><category><id>344
</id><name>Arts &amp; Entertainment &gt; Artwork &gt; Prints
</name>
</category><category><id>345
</id><name>Arts &amp; Entertainment &gt; Artwork &gt; Weatherprints &amp; Outdoor Art
</name>
</category><category><id>346
</id><name>Arts &amp; Entertainment &gt; Collectibles
</name>
</category><category><id>347
</id><name>Arts &amp; Entertainment &gt; Collectibles &gt; Autographs
</name>
</category><category><id>340
</id><name>Arts &amp; Entertainment &gt; Artwork &gt; Paintings
</name>
</category><category><id>341
</id><name>Arts &amp; Entertainment &gt; Artwork &gt; Photographs
</name>
</category><category><id>342
</id><name>Arts &amp; Entertainment &gt; Artwork &gt; Photographs &gt; Stock Photographs
</name>
</category><category><id>343
</id><name>Arts &amp; Entertainment &gt; Artwork &gt; Posters &amp; Reproductions
</name>
</category><category><id>2918
</id><name>Home &amp; Garden &gt; Lawn &amp; Garden &gt; Outdoor Living &gt; Patio Umbrellas &amp; Sunshades
</name>
</category><category><id>3996
</id><name>Sporting Goods &gt; Racquet Sports &gt; Tennis &gt; Tennis Ball Savers
</name>
</category><category><id>3995
</id><name>Sporting Goods &gt; Racquet Sports &gt; Tennis &gt; Tennis Ball Launchers
</name>
</category>

只是一堆类别,它只是证明字符串是完全不可读的,有没有办法实际将这个XML响应作为一个带有curl的数组?它是一个直接数组还是需要以手动方式将字符串转换为数组,如果是,那么最好的程序是什么。

2 个答案:

答案 0 :(得分:0)

CURL无法对XML做任何事情。一些API将为您提供seralised数组和JSON响应。检查两者是否更容易处理。否则,您将需要使用PHP DOM来读取XML并将其转换为数组:

http://www.php.net/book.dom

What is the best php DOM 2 Array function?

答案 1 :(得分:0)

您应该使用SimpleXML扩展名和simplexml_load_string函数从字符串中创建XML对象。之后,您可以像使用任何其他对象一样简单地使用它并将其转换为数组(如果需要)。