Fetch data from websites using XPATH and PHP

xpath

xpath and PHPThis video is around XPATH and PHP. XPath is an element in the XSLT standard. I Over here our matter of interest is to use XPATH with PHP server scripting language to fetch some required information from websites, like youtube subscriber count, twitter followers etc. So if you are interested in making something like live facebook or twitter follower counters you can use it to do the job. You can upload the script to an online server or you can use it on your local server or you can even store the file to the raspberry pi server. And if you are new to server and PHP you can check out my earlier videos on Making a raspberry pi server or installing XAMPP on your PC for which I have provided the links down below in the description. After putting the script to the server you just need to call the page to fetch the required data. You can do so from any browser or even if any microcontrollers. In this video, I will show you how to retrieve twitter followers and youtube subscribers count from my channel.


Code: bit.ly/2mQc75X

 

<?php
function tidy_html($input_string) {
         
  $config = array('output-html'   => true,'indent' => true,'wrap'=> 800); 
            
    // Detect if Tidy is in configured    
    if( function_exists('tidy_get_release') ) {
        $tidy = new tidy;
        $tidy->parseString($input_string, $config, 'raw');
        $tidy->cleanRepair();
        $cleaned_html  = tidy_get_output($tidy); 
        } 
  else {
        # Tidy not configured for this Server
        $cleaned_html = $input_string;
  }
    return $cleaned_html;
}

function getFromPage($webAddress,$path){
  $source = file_get_contents($webAddress); //download the page 
  $clean_source = tidy_html($source);
  $doc = new DOMDocument;

  // suppress errors
  libxml_use_internal_errors(true);

  // load the html source from a string
  $doc->loadHTML($clean_source);
  $xpath = new DOMXPath($doc);
  $data="";
  $nodelist = $xpath->query($path);
  $node_counts = $nodelist->length; // count how many nodes returned
  if ($node_counts) { // it will be true if the count is more than 0
    foreach ($nodelist as $element) {
           $data= $data.$element->nodeValue . "\n";
    }
  }
  return $data;
  
}
echo getFromPage("http://www.youtube.com/weargenius","//*[@id=\"c4-primary-header-contents\"]/div/div/div[2]/div/span/span[1]");
echo "</br>";
echo getFromPage("http://www.twitter.com/geekybikash","//*[@id=\"page-container\"]/div[1]/div/div[2]/div/div/div[2]/div/div/ul/li[3]/a/span[2]");
?>

 

Setting up XAMPP Server: bit.ly/2mQ4NHt
Setting up PI as Server: bit.ly/2lR7k4N

Tidy HTML PHP :bit.ly/2mgvoKq
XPATH Details: bit.ly/2ntC9cd

********************************************************************
Subscribe YouTube: goo.gl/FhfdL7

Guys Subscribe to my channel for latest contents into your inbox.
Support me to keep going.

Support me on Patreon: bit.ly/2jcjTSo
——————————————————————————–
Website: www.weargenius.in
TwTwitter:
YouTube: www.youtube.com/weargenius
Instagram: www.instagram.com/weargenius/
GIT: github.com/oksbwn
Facebook: www.facebook.com/geekybikash

Bikash Panda
Catch Me On

Bikash Panda

Blogger / Embedded System Developer at WGLabz
A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.
Bikash Panda
Catch Me On

Latest posts by Bikash Panda (see all)

Related posts