A PHP class for scraping Trustpilot account reviews.
- Number of reviews to return.
- Increasing and decreasing sorting by date and rating.
- Export reviews to a csv or xml file.
Instantiates the class:
require_once 'class-trustpilot-reviews.php';
$scraper = new Trustpilot_Reviews('account_id');
Parameter | Type | Description | Default |
---|---|---|---|
$id |
string |
Required. ID of Trustpilot account. | |
$count |
int |
Defines the number of reviews to return. '-1' returns all reviews. | -1 |
$orderby |
string |
Defines by which parameter to sort reviews. Accepts: 'time' or 'rating' | time |
$order |
string |
Designates ascending or descending order of reviews. Accepts 'asc', 'desc' | desc |
Return array of reviews
$scraper->get_reviews();
Generate xml of reviews.
$scraper->generate_xml($path);
Parameter | Type | Description |
---|---|---|
path |
string |
Required. Path to save generated xml file. |
Generate csv of reviews.
$scraper->generate_xml($path, $separator);
Parameter | Type | Description | Default |
---|---|---|---|
path |
string |
Required. Path to save generated csv file. | |
separator |
string |
The optional separator parameter sets the field delimiter. | ',' |
What optimizations did you make in your code? E.g. refactors, performance improvements, accessibility
The process of recovering reviews can be quite long depending on the number of reviews to be recovered. A good strategy would be to temporarily save reviews in the db or cache, and retrieve them again only when needed.
Se utilizzi WordPress una buona strategia potrebbe essere sfruttare i transients.
<?php
if ( false === ( $reviews = get_transient( 'trustpilot_reviews' ) ) ) {
$scraper = new Trustpilot_Reviews('account_id', '-1', 'time', 'desc');
$reviews = $scraper->get_reviews();
set_transient( 'trustpilot_reviews', $reviews, 24 * HOUR_IN_SECONDS );
}
?>
In this case the reviews are saved in a transient with validity of 24 hours in the database. In this way, instead of performing the whole process of scraping and parsing, as long as the transient is valid, they are recovered directly from the database, and only when the transient expires they will be scraped again.