getButterfly Logo getButterfly

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).


Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.



If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

Leave a reply