Welcome Guest, Not a member yet? Register   Sign In
Simple library for retrieving Stumbleupon views for a particular URL
#1

[eluser]Vheissu[/eluser]
I recently need to get the number of views a Stumbleupon submission had received, and decided to write a small little helper class. I thought I would offer it up as someone else might find it useful for displaying the number of views their article got on Stumbleupon or whatever else.

I suggest if you use it with Codeigniter, drop it into your libraries folder and use it that way. Copy and paste the below code to a new PHP file. There is an example at the end of the file for it's use.

Code:
<?php
/*
* This script will fetch the number of views a particular URL has received (if it is on StumbleUpon).
* Script created by: Dwayne - http://www.ilikekillnerds.com
*/

class Stumbleviews {

    private $curl;
    private $userAgent = "Googlebot/2.1 (http://www.googlebot.com/bot.html)";
    private $targetUrl = "http://www.stumbleupon.com/url/";
    private $useCurl   = true;

    public function __construct() {
    }
    
    public function set_useragent($useragent)
    {
        $this->userAgent = $useragent;
    }
    
    public function set_method($method)
    {
        if ($method == 'curl') {
            $this->useCurl = true;
        }
        
        if ($method == 'file') {
            $this->useCurl = false;
        }
        
    }
    
    public function set_url($url) {
        $this->targetUrl = $this->targetUrl . trim($url);
    }

    public function fetch_views() {
        
        if ($this->useCurl == true) {
            $this->_prep(); // Setup our curl options
            $html = curl_exec($this->curl);
        } else {
            $html = file_get_contents($this->targetUrl);
        }

        $dom = new DOMDocument();
        @$dom->loadHTML($html);

        // New XPath for querying our HTML
        $xpath = new DOMXPath($dom);
        $views = $xpath->evaluate("//div[@class='views']//a[@href='$this->targetUrl']");

        // Return the number of views
        for ($i = 0; $i < $views->length; $i++) {
            $view = $views->item($i)->nodeValue;
            return $view;
        }

    }
    
    private function _prep()
    {
        $this->curl = curl_init();
        curl_setopt($this->curl, CURLOPT_URL, $this->targetUrl);
        curl_setopt($this->curl, CURLOPT_USERAGENT, $this->userAgent);
        curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
        curl_setopt($this->curl, CURLOPT_HEADER, 1);
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
    }

}

/**
* Example code shown below
* We do not provided the www. or anything else
* you only need to provide the site and extension
* as you can see below for google all we need is
* google.com
*
    $stumbleviews = new Stumbleviews();
    $stumbleviews->set_url('google.com');
    echo $stumbleviews->fetch_views();
*/

?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB