Skip to content

Instantly share code, notes, and snippets.

@Sam-R
Created September 16, 2020 09:31
Show Gist options
  • Save Sam-R/51959b1fa1e50c6b73a48ca4d4ddb02e to your computer and use it in GitHub Desktop.
Save Sam-R/51959b1fa1e50c6b73a48ca4d4ddb02e to your computer and use it in GitHub Desktop.
Get the number of stars from a given TrustPilot page using PHP
<?php
// Webpage to load
$url = "https://uk.trustpilot.com/review/your-page-here";
// Contents of webpage
$contents = file_get_contents($url);
// Declair a new DOM
$doc = new DOMDocument('1.0', 'UTF-8');
// Disable errors, storing original state
$internal_errors = libxml_use_internal_errors(true);
// Load the HTML document into DOMDoc
$doc->loadHTML($contents);
// Create a new DOM path
$xpath = new DomXPath($doc);
// Look for this class in the DOM
$class_name="header_trustscore";
// Query the DOM for all instances of that class
$node_list = $xpath->query(sprintf('//*[@class="%s"]', $class_name));
// Display the first matching node's text content
print_r($node_list->item(0)->textContent);
// Restore error state
libxml_use_internal_errors($internal_errors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment