-1

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.

lbennet
  • 1,083
  • 5
  • 14
  • 31
  • 1
    I wouldn't post your access tokens here. Anyone can see it and use it. The only way to fix it is to delete the post and recreate it. – Machavity Dec 22 '13 at 01:54
  • yes, I actually had modified the key values to a non existent one for a security measure – lbennet Dec 22 '13 at 01:58

2 Answers2

0

CURL cannot do anything with the XML. Some APIs will give you seralised array and JSON responses. Check for those as both are much easier to deal with. Otherwise you'll need to use PHP DOM to read the XML and make it into an array:

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

What is the best php DOM 2 Array function?

Community
  • 1
  • 1
Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48
0

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).

Dmitry Astrikov
  • 637
  • 1
  • 6
  • 16