getButterfly Logo

How to get Trustpilot reviews based on tags and/or stars

Trustpilot reviews

In order to use this code, you need a Trustpilot business account. You also need to manually request your API credentials. The script below is working as intended as of June 17th, 2015.

<?php
/*
 * Copyright 2015 Ciprian Popescu (email: getbutterfly@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

// fill in these details
$tp_unit_id = ''; // Unit ID
$tp_api_key = ''; // Public API
$tp_api_secret = ''; // Customer API (Secret)
$tp_username = ''; // Trustpilot Username (Email Address)
$tp_password = ''; // Trustpilot Password
//

$tp_api = base64_encode($tp_api_key . ':' . $tp_api_secret);
$url = 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken';
$data = array(
    'grant_type' => 'password',
    'username' => $tp_username,
    'password' => $tp_password
);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic $tp_api\r\n",
        'method' => 'POST',
        'content' => http_build_query($data),
    ),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

$result = json_decode($result, true);
$access_token = $result['access_token'];

$url = 'https://api.trustpilot.com/v1/private/business-units/' . $tp_unit_id . '/reviews?token=' . $access_token . '';
$data = array(
    'grant_type' => 'password',
    'username' => $tp_username,
    'password' => $tp_password
);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic $tp_api\r\n",
        'method' => 'GET',
        'content' => http_build_query($data),
    ),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

$result = json_decode($result, true);
echo '<div id="reviews">';
    $i = 0;
    $t = array(); 
    foreach($result['reviews'] as $res) {
        foreach($res['tags'] as $v) {
            $t[] = $v['value'];
        }
        if(in_array('some-tag-here', $t)) { // comment this line out if you don't want to restrict reviews based on tag
            if($i < 30 && ($res['stars'] == 4 || $res['stars'] == 5)) {
                echo '<div class="item">';
                    for($s = 1; $s <= $res['stars']; ++$s) {
                        echo '<img src="//widget.trustpilot.com/widgets/539ad0ffdec7e10e686debd7/' . $tp_unit_id . '/en-US/sprite_star.png" width="22" height="22"> ';
                    }
                    echo '<h4>' . $res['title'] . '</h4>';
                    echo '<p>' . $res['text'] . '</p>';
                    echo '<p>' . $res['consumer']['displayName'] . '</p>';
                    echo '<p>' . $res['stars'] . ' stars</p>';
                echo '</div>';
            }
            ++$i;
        } //
        unset($t); $t = array(); // reset the tag array
    }
echo '</div>';
?>

You may be required to change some details, such as tags, number of stars and the total number of displayed reviews (mine is 30).


Leave a Reply

Your email address will not be published. Required fields are marked *