Skip to content

Instantly share code, notes, and snippets.

@vaibhavhrt
Last active April 14, 2018 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavhrt/0e5d340f4389bee5f7cc741efad70ac4 to your computer and use it in GitHub Desktop.
Save vaibhavhrt/0e5d340f4389bee5f7cc741efad70ac4 to your computer and use it in GitHub Desktop.
code to get text, href, etc data from any webpage using simple_html_dom in php
<?php
$pageURL = "http://www.google.com"; //replace with the url of page you wanna access
require("simple_html_dom.php"); //download simple_html_dom parser from here: "http://simplehtmldom.sourceforge.net/" and put it in the same location as this .php file
$html = file_get_html($pageURL);
foreach($html->find('a[class=xyz]') as $element){ //replace a[class=xyz] with the elemets you want to get, read here for more info: http://simplehtmldom.sourceforge.net/manual.htm
echo $element->href . '<br>'; //display the href just for testing purposes
} //$element is an object with all the attributes of all matching elements according to your selector, do whatever you want with it.
// if getting this error: Warning: file_get_contents(): stream does not support seeking in simple_html_dom.php on line 76,
//On line 76 of simple_html_dom.php:
// $contents = file_get_contents($url, $use_include_path, $context, $offset);
//Remove the reference to $offset:
// $contents = file_get_contents($url, $use_include_path, $context);
//see here for more info: https://stackoverflow.com/questions/42685814/file-get-contents-stream-does-not-support-seeking-when-was-php-behavior-abo
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment