0

i am having this code that is used for parsing a xml url and stores output in the $query variable.

 $url = 'http://www.slideshare.net/api/2/search_slideshows?api_key=GRCsS0fPo&
 ts=1320547556&hash=cf362f863d1f15e84c7a520804f7826731f958c4&q=electrical+engineering&
 page=4&download=0&items_per_page=25';

 echo $url;

 $ch=curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Your application name');

$query = curl_exec($ch);

curl_close($ch);
$array = (array) simplexml_load_string($query);
echo '<pre>';
print_r($array);

when i print this array using print_r($array) it gives me

Array
   (
   [Meta] => SimpleXMLElement Object
    (
     [Query] => php programming
      [ResultOffset] => SimpleXMLElement Object
        (
        )

      [NumResults] => 25
      [TotalResults] => 36839
      )

      [Slideshow] => Array
     (
          [0] => SimpleXMLElement Object
        (
            [ID] => 1966058
            [Title] => title here
            [Description] => description here
            [Status] => 2
            [Username] =>usrname
            [URL] => url here
            [ThumbnailURL] => a url
            [ThumbnailSmallURL] => a url
               [Embed] => some embed code
    )
    [1] => SimpleXMLElement Object
        (
            [ID] => 1966058
            [Title] => title here
            [Description] => description here
            [Status] => 2
            [Username] =>usrname
            [URL] => url here
            [ThumbnailURL] => a url
            [ThumbnailSmallURL] => a url
            [Embed] => some embed code
    )

now i want to fetch this array and use its every value. i want to all nodes values of this array. so how to fetch this array using foreach loop.

1

1 Answer 1

0

I think it would be better to use the SimpleXML XPath method rather than iterate over the array directly.

Sign up to request clarification or add additional context in comments.

2 Comments

foreach ($array->xpath('//Slideshow') as $slideShow) { echo $slideShow->Username; }
this is working fine but now an another problem has occured that the most of the time the array provided by curl becomes null. what is the problem. i am running the whole script on localhost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.