Fetch data from websites using XPATH and PHP


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: http://bit.ly/2mQc75X


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');
        $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

  // load the html source from a string
  $xpath = new DOMXPath($doc);
  $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: http://bit.ly/2mQ4NHt
Setting up PI as Server: http://bit.ly/2lR7k4N

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

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

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

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

Related posts

One Thought to “Fetch data from websites using XPATH and PHP”

  1. […] WeMOS: bit.ly/2oTtoty Arduino HTTP Call (ESP8266): bit.ly/2r8yKBp Fetch data using XPATH and PHP: bit.ly/2pGB0zC Programming Esp8266 using Arduino IDE: bit.ly/2hdARij Github Repository: bit.ly/2r8GFhX Schematic: […]

Leave a Comment