我正在使用这个卷曲代码:
$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 & Parts > Automotive Monitoring > Radar Detectors & Jammers
</name>
</category><category><id>4458
</id><name>Vehicles & Parts > Automotive Parts > Automotive Batteries > Fuel Cells
</name>
</category><category><id>4412
</id><name>Vehicles & Parts > Automotive Exterior > License Plates
</name>
</category><category><id>3734
</id><name>Sporting Goods > Outdoor Recreation > Camping, Backpacking & Hiking > Camp Furniture
</name>
</category><category><id>4447
</id><name>Vehicles & Parts > Automotive Monitoring > Speedometers
</name>
</category><category><id>4026
</id><name>Sporting Goods > Team Sports > Beach Volleyball > Beach Volleyball Nets
</name>
</category><category><id>3724
</id><name>Sporting Goods > Outdoor Recreation > Archery > Archery Armguards
</name>
</category><category><id>4024
</id><name>Sporting Goods > Team Sports > Beach Volleyball
</name>
</category><category><id>4025
</id><name>Sporting Goods > Team Sports > Beach Volleyball > Beach Volleyball Balls
</name>
</category><category><id>4022
</id><name>Sporting Goods > Team Sports > Basketball > Basketball Hoops
</name>
</category><category><id>4023
</id><name>Sporting Goods > Team Sports > Basketball > Basketballs
</name>
</category><category><id>4020
</id><name>Sporting Goods > Team Sports > Basketball > Basketball Hoop Parts & Accessories > Basketball Poles
</name>
</category><category><id>3725
</id><name>Sporting Goods > Outdoor Recreation > Archery > Archery Targets
</name>
</category><category><id>3798
</id><name>Sporting Goods > Outdoor Recreation > Cycling > Bicycle Parts > Bicycle Forks
</name>
</category><category><id>4028
</id><name>Sporting Goods > Team Sports > Cheerleading > Cheerleading Megaphones
</name>
</category><category><id>4029
</id><name>Sporting Goods > Team Sports > Cheerleading > Pom-Poms
</name>
</category><category><id>4607
</id><name>Vehicles & Parts > Watercraft Parts & Accessories > Watercraft Care > Watercraft Polishes
</name>
</category><category><id>4417
</id><name>Vehicles & Parts > Automotive Exterior > Winch Parts
</name>
</category><category><id>344
</id><name>Arts & Entertainment > Artwork > Prints
</name>
</category><category><id>345
</id><name>Arts & Entertainment > Artwork > Weatherprints & Outdoor Art
</name>
</category><category><id>346
</id><name>Arts & Entertainment > Collectibles
</name>
</category><category><id>347
</id><name>Arts & Entertainment > Collectibles > Autographs
</name>
</category><category><id>340
</id><name>Arts & Entertainment > Artwork > Paintings
</name>
</category><category><id>341
</id><name>Arts & Entertainment > Artwork > Photographs
</name>
</category><category><id>342
</id><name>Arts & Entertainment > Artwork > Photographs > Stock Photographs
</name>
</category><category><id>343
</id><name>Arts & Entertainment > Artwork > Posters & Reproductions
</name>
</category><category><id>2918
</id><name>Home & Garden > Lawn & Garden > Outdoor Living > Patio Umbrellas & Sunshades
</name>
</category><category><id>3996
</id><name>Sporting Goods > Racquet Sports > Tennis > Tennis Ball Savers
</name>
</category><category><id>3995
</id><name>Sporting Goods > Racquet Sports > Tennis > Tennis Ball Launchers
</name>
</category>
只是一堆类别,它只是证明字符串是完全不可读的,有没有办法实际将这个XML响应作为一个带有curl的数组?它是一个直接数组还是需要以手动方式将字符串转换为数组,如果是,那么最好的程序是什么。
答案 0 :(得分:0)
CURL无法对XML做任何事情。一些API将为您提供seralised数组和JSON响应。检查两者是否更容易处理。否则,您将需要使用PHP DOM来读取XML并将其转换为数组:
答案 1 :(得分:0)
您应该使用SimpleXML扩展名和simplexml_load_string函数从字符串中创建XML对象。之后,您可以像使用任何其他对象一样简单地使用它并将其转换为数组(如果需要)。