douyinghuo8874 2013-12-22 01:51
浏览 131
已采纳

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

I am using this curl code:

$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);

Which gives me the following string

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>

Just a bunch of categories, it just turns out that the string would be totally unreadable, is there a way to actually get this XML response as an array with curl ? A direct array from it or does it require conversion of the string to an array in a manual way, if so, what's the best procedure.

  • 写回答

2条回答 默认 最新

  • doudu9652 2013-12-22 02:06
    关注

    You should use the SimpleXML extension and simplexml_load_string function to create a XML object from your string. After that you can simple use it like an any other object and transform it into an array (if you want).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据