Skip to main content

How to get data from XML file in PHP?

Get data from XML file in PHP



$rss_link = "http://xyogasangeetax.api.channel.livestream.com/2.0/latestclips.xml"; //xml_file_link   

// Load xml data.
$xml = file_get_contents($rss_link);
// Strip whitespace between xml tags
$xml = preg_replace('~\s*(<([^>]*)>[^<]*</\2>|<[^>]*>)\s*~','$1',$xml);
// Convert CDATA into xml nodes.
$rss_feed = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA

$rss_feed_channel = $rss_feed->channel;

$rss_feed_channel_item = $rss_feed->channel->item 

                                                         or

$rss_link = "http://xyogasangeetax.api.channel.livestream.com/2.0/latestclips.xml"; //xml_file_link   

//$rss_feed = simplexml_load_file($rss_link, 'SimpleXMLElement', LIBXML_NOCDATA);

   $rss_feed = file_get_contents($rss_link);

   //mb_convert_encoding($rss_feed, 'UTF-16LE', 'UTF-8');

   $rss_feed = new SimpleXmlElement($rss_feed); 

$rss_feed_channel = $rss_feed->channel;

$rss_feed_channel_item = $rss_feed->channel->item 


or

$context = stream_context_create( array( 'http' => array( 'follow_location' => false ) ) ); 

$content = file_get_contents("http://xyogasangeetax.api.channel.livestream.com/2.0/latestclips.xml", false, $context); 

$data = new SimpleXmlElement($content); 

foreach($data->channel->item as $entry) 

    { if ($media = $entry->children('media', TRUE)) 

        { echo "<div style=\"width:160px;display:block;float:left;padding:15px;\">"; 

        $attributes = $media->content->attributes(); 

        $src = $play_attributes['url']; 

            if ($media->thumbnail) 

                    { $attributes = $media->thumbnail->attributes(); 

                    $imgsrc = (string)$attributes['url']; 

                    echo "<img src=\"$imgsrc\" alt=\"\" \/>"; 

                    } 

        } 

$pub_date= explode("-",$entry->pubDate); 

echo date('F d,Y',strtotime(trim($pub_date[0]))); 

echo "</div>"; }

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...