3

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>
          <h2>Testing 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.

  • I am not really sure if I am taking the best approach possible here but after researching and reading through the HTTP API documentation codex.wordpress.org/HTTP_API i tried the code above which gets the contents of the page of the url. This seems to work, but now I am wondering how I would go about extract and display only specific parts of the content I am getting from the url. Ideally I would have all the content on $body in an array and then echo only the array items I want to use. In my mind I think this would be the best way to achieve this but I just not sure how to do it. – Madeirense Oct 7 '16 at 10:58
  • 1
    If the page from which you are fetching content has made sections (section with anchor tag) then you might get specific section by adding its ID in url, like your url in question $url = 'http://www.bbc.co.uk/news/';. If you want a section from that page then put section ID like this $url = 'http://www.bbc.co.uk/news/#section_id';. You have to find out section id first, for that you can use fire bug (or related tool). And off-course its just a wild guess! To know about sections in detail, read this computerhope.com/issues/ch000049.htm – Rishabh Oct 7 '16 at 11:06
  • Thank you for your suggestion. But I don't think i have explained myself properly. what I am trying to achieve here is a bit different. Let's imagine I use this page url as my $url. With the set up I have at the moment I will get the whole content of this page. What I am trying to do is then access specific content from this page. So, if i use this page url as my $url how would I go about getting just the title of the question in this page "How to consume and display external data in WordPress from another website" for example? – Madeirense Oct 7 '16 at 11:43
  • Hmm you are saying the url that your mentioned in your question (http://www.bbc.co.uk/news/) is example of your site's url? If yes then sorry I thought its the url of external site which you are using to fetch data! – Rishabh Oct 7 '16 at 12:07
  • No, The code above will be on my website and the url on the $url will be another website from where I will be fetching my data. – Madeirense Oct 7 '16 at 12:15
0

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!!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.