Welcome Guest, Not a member yet? Register   Sign In
Grab pins from Pinterest
#1

[eluser]Flemming[/eluser]
My client wants to display their Pinterest pins on their company website homepage.

After realising there isn't yet a stable and well-documented API for Pinterest I came up with this, as Pinterest does expose an RSS feed which includes everything you need in order to display the images on your site. Perhaps this will be useful to someone else (I'm using CI 2.1).


1. Download the atomizer spark (if you haven't used CI sparks before they can be implemented in 2 ways, I just download the zip and put the files in the right places in my CI structure)

2. pop this into your controller:
Code:
public function update_pins()
    {
        $this->load->library('atomizer');

        $url = 'http://pinterest.com/farnott/feed.rss'; // put your pinterest username here
        $feed = $this->atomizer->load( file_get_contents( $url ) );

        foreach( $feed->channels as $channel ) {

            $items = $channel->items;

            foreach( $items as $item )
            {
                echo $item->title . '<br />';
                echo $item->link . '<br />';
                echo $item->description . '<br />';
                echo 'Title: ' . $this->get_pinterest_title($item->description) . '<br />';
                echo 'Img Src: ' . $this->get_pinterest_img_src($item->description) . '<br />';
                echo $item->pubDate . '<hr />';
            }
        }
    }

    private function get_pinterest_img_src($html)
    {
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        return $dom->getElementsByTagName('img')->item(0)->getAttribute('src');

    }

    private function get_pinterest_title($html)
    {
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        $text = $dom->getElementsByTagName('p');

        foreach ($text as $tag)
        {
            $ret[] = $tag->nodeValue;
        }

        return $ret[1];

    }

3. visit yoursite.com/controller/update_pins - this will echo out the stuff you need. I created a couple of helper functions (get_pinterest_img_src / get_pinterest_title) so that I can extract exactly the bits I need so I can store them in a DB table.

If you can simplify or improve or make suggestions, please do!




Theme © iAndrew 2016 - Forum software by © MyBB