; Trustedshops | Jung Edelstahl Design

Trustedshops

/**
This is an example of integrating Trusted Shops Customer Reviews
marked with structured data into your website
Make sure you replace value of $tsid with your Trusted Shops ID
Make sure you have a writing permission to the
folder set in $cacheFileName
Requires version PHP 5.+
*/
$tsId = ‘XC6A7A8C91B0E24F2AE881B03116C9561’;
$cacheFileName = ‘/tmp/’ . $tsId . ‘.json’;
$cacheTimeOut = 43200; // half a day
$apiUrl = ‘http://api.trustedshops.com/rest/public/v2/shops/’ . $tsId . ‘/quality/reviews.json’;
$reviewsFound = false;
if (!function_exists(‘trustedshopscachecheck’)) {
function trustedshopscachecheck($filename_cache, $timeout = 10800) {
if (file_exists($filename_cache) && time() – filemtime($filename_cache) < $timeout) { return true; } return false; } } // check if cached version exists if (!trustedshopscachecheck($cacheFileName, $cacheTimeOut)) { // load fresh from API $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_URL, $apiUrl); $output = curl_exec($ch); curl_close($ch); // Write the contents back to the file // Make sure you can write to file’s destination file_put_contents($cacheFileName, $output); } if ($jsonObject = json_decode(file_get_contents($cacheFileName), true)) { $result = $jsonObject[‘response’][‘data’][‘shop’][‘qualityIndicators’][‘reviewIndicator’][‘overallMark’]; $count = $jsonObject[‘response’][‘data’] [‘shop’][‘qualityIndicators’][‘reviewIndicator’][‘activeReviewCount’]; $shopName = $jsonObject[‘response’][‘data’][‘shop’][‘name’]; $max = “5.00”; if ($count > 0) {
$reviewsFound = true;
}
}
if ($reviewsFound) {
// Output aggregated reviews markup:
$ts_snippet = ‘<script type=”application/ld+json”>’;
$ts_snippet .= ‘{‘;
$ts_snippet .= ‘”@context”: “http://schema.org”,’;
$ts_snippet .= ‘”@type”: “Organization”,’;
$ts_snippet .= ‘”name”: “‘.$shopName.'”,’;
$ts_snippet .= ‘”aggregateRating” : {‘;
$ts_snippet .= ‘”@type”: “AggregateRating”,’;
$ts_snippet .= ‘”ratingValue” : “‘.$result.'”,’;
$ts_snippet .= ‘”bestRating” : “‘.$max.'”,’;
$ts_snippet .= ‘”ratingCount” : “‘.$count.'”‘;
$ts_snippet .= ‘}}’;
$ts_snippet .= ”;
echo $ts_snippet;
}