1

I read many stackoverflow question and I'm using this code but I don't know why this is not work.
Here is a code.

$url = 'http://m.cricbuzz.com/cricket-schedule';
$source = file_get_contents($url);

$doc = new DOMDocument;
@$doc->loadHTML($source);

$xpath = new DOMXPath($doc);
$classname = "list-group";
$events = $xpath->query("//*[contains(@class, '$classname')]");

var_dump($xpath);

Can you please check it why this is not working actually I want to get data from list-group

Azeem Haider
  • 1,443
  • 4
  • 23
  • 41

1 Answers1

1

The code is correct. It correctly fetches a list of DOM nodes having the specified class attribute value into the $events variable:

$events = $xpath->query("//*[contains(@class, '$classname')]");

which is an instance of DOMNodeList. Next you should iterate the list and fetch the data you need from $events. For example, if you need the outer HTML for the nodes, use something like this:

foreach ($events as $e) {
  printf("<<<<<\n%s\n>>>>>\n", $e->ownerDocument->saveXML($e));
}

P.S.: I would rename $events to $elements.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • this code is not working for me i checked it in `var_dump` it give me this `object value omitted` – Azeem Haider Nov 19 '16 at 08:44
  • Is there any problem in my wamp server – Azeem Haider Nov 19 '16 at 08:44
  • @AzeemHaider, check out [this](https://eval.in/680762) fiddle. I have replaced `file_get_contents()` with a here document as the server doesn't allow it. Also try `echo` or `printf` instead of `var_dump`. You may have some extension installed that overrides the behavior of `var_dump` (`xdebug`, I guess) – Ruslan Osmanov Nov 19 '16 at 08:49