How to consume and display external data in WordPress from another website

I am trying to access data from another website to display on a WordPress Website I am developing. So far I have the following:

?php
    /*
       Template Name: Testing remote data
    */

get_header(); 

div class="main"
    div class="col-sm-12"
      header
          h2Testing remote data/h2
      /header
    /div
    div class="container"
        div id="content" role="main"
            div class="col-sm-12"
                ?php
                    $url = 'http://www.bbc.co.uk/news/';// this url is only for example purposes
                    $request = wp_remote_get( $url );

                    if(is_wp_error($request)) {
                        return false;
                    } else {
                        $body = $request['body'];
                    }

                    echo $body;

            /div
         /div
     /div
 /div

This works fine. However, I get the whole body content. How would I go about getting specific sections of the body? If any one could help me with this I would really appreciate it. Sorry if it's an obvious one but I am new to WordPress and I am still far from comfortable with it.

Topic wp-remote-get http-api Wordpress

Category Web


// make request... (optionally save in transient for faster future fetches)
$dom = new DOMDocument();
$dom->loadHTML(wp_remote_retrieve_body($request));
$sections=$dom->getElementsByTagName("section");
foreach ($sections as $section) {
    // Do something...
    }

you can access data from other website via RSS feed. You can see the RSS feed of bbc news in the following url
http://news.bbc.co.uk/2/hi/help/rss/default.stm

then you could incorporate it feed in your site using transient so that you can set the appropriate time to fetch the new data. And with the DOMDocument element you can get the values/data

Below is just a sample function where i have prepare a function to take your RSS feed url.

function vp_get_rss_feed($feed_url) {
    $expires = 7200; // 2hours

    delete_transient( 'rss_bbc_feed_world' );
    $feed = get_transient( 'rss_bbc_feed_world' );
    if ( false === ( $rss = $feed ) ) :
        $rss = new DOMDocument();
        $rss->load($feed_url);

        $feed = array();
        $i=1;
        foreach ($rss->getElementsByTagName('item') as $node) {

            $link = $node->getElementsByTagName('link')->item(0)->nodeValue;
            $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
            $slug = sanitize_title($title);
            $desc = $node->getElementsByTagName('description')->item(0)->nodeValue;


            $item[$slug] = array (
                            'title' => $title,
                            'slug' => $slug,
                            'desc' => $desc,
                            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                        );

        }
        array_push( $feed, $item );
        set_transient( 'rss_bbc_feed_world', $feed, $expires );
    endif;
    return $feed;
}

This function takes your RSS feed and store on a array and return that array( $feed )

You can then loop through that $feed in your desire location to where you would like to show the RSS feed data in your desire html styles.

FOR REFERENCE:
https://codex.wordpress.org/Transients_API
http://php.net/manual/en/class.domdocument.php

Hope that helps!!

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.