olape wrote:cmb wrote:Wie ein anderer Handler verwendet werden kann, ist in der Guzzle-Doku beschrieben. Das müsste dann nur vom Shariff-Backend unterstützt werden (anscheinend müsste Backend.php modifiziert werden).
Ich wusste schon, warum ich eigentlich nichts mit dem Backend zu tun haben wollte.
Verständlich, wenn man die Komplexität des Shariff-Backends bedenkt. Diese Komplexität ist sinnvoll, damit das Backend sehr flexibel verwendet werden kann, und auch auf High-Traffic-Websites genutzt werden kann. Aber auf einer Website mit ein paar Hundert oder weniger Page-Views täglich (so wie das wohl bei CMSimple_XH Websites üblich ist), braucht man weder alternative Caches, geschweige denn alternative Cache-Adapter (z.B. memcache), noch sonst eine der Hochleistungskomponenten. Im Prinzip genügt schon fast folgendes Backend (index.php):
Code: Select all
<?php
header('Content-Type:application/json');
// these should be taken from config.php
$services = array('Facebook', 'LinkedIn', 'Xing', 'GooglePlus');
if (!isset($_GET['url'])) {
echo '[]';
exit;
}
$url = $_GET['url'];
$timeout = 5;
$ttl = 300;
$cache = __DIR__ . '/' . md5($url) . '.json';
if (file_exists($cache) && filemtime($cache) >= time() - $ttl) {
$result = file_get_contents($cache);
} else {
$result = array();
if (in_array('Facebook', $services)) {
$query = urlencode('SELECT total_count FROM link_stat WHERE url="' . $url . '"');
$query = 'https://graph.facebook.com/fql?q=' . $query;
$opts = array('http' => array('timeout' => $timeout));
$context = stream_context_create($opts);
$response = json_decode(file_get_contents($query, false, $context), true);
if (isset($response['data'][0]['total_count'])) {
$result['facebook'] = $response['data'][0]['total_count'];
}
}
if (in_array('LinkedIn', $services)) {
$query = 'https://www.linkedin.com/countserv/count/share?url=' . urlencode($url) . '&lang=de_DE&format=json';
$opts = array('http' => array('timeout' => $timeout));
$context = stream_context_create($opts);
$response = json_decode(file_get_contents($query, false, $context), true);
if (isset($response['count'])) {
$result['linkedin'] = $response['count'];
}
}
if (in_array('Xing', $services)) {
$query = 'https://www.xing-share.com/spi/shares/statistics?url=' . urlencode($url);
$opts = array('http' => array('method' => 'POST', 'timeout' => $timeout));
$context = stream_context_create($opts);
$response = json_decode(file_get_contents($query, false, $context), true);
if (isset($response['share_counter'])) {
$result['xing'] = $response['share_counter'];
}
}
if (in_array('GooglePlus', $services)) {
$query = 'https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ';
$json = array(
'method' => 'pos.plusones.get',
'id' => 'p',
'params' => array(
'nolog' => 'true',
'id' => $url,
'source' => 'widget',
'userId' => '@viewer',
'groupId' => '@self',
),
'jsonrpc' => '2.0',
'key' => 'p',
'apiVersion' => 'v1',
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-Type: application/json', 'content' => json_encode($json), 'timeout' => $timeout));
$context = stream_context_create($opts);
$response = json_decode(file_get_contents($query, false, $context), true);
if (isset($response['result']['metadata']['globalCounts']['count'])) {
$result['googleplus'] = $response['result']['metadata']['globalCounts']['count'];
}
}
$result = json_encode($result);
file_put_contents($cache, $result);
}
echo $result;
Da sind bisher nur 4 Services überhaupt implementiert (und diese werden nicht durch config.php beeinflusst), die Requests werden synchron gestellt (daher das kurze $timeout), das Caching ist ziemlich primitiv realisiert, und der Code alles andere als clean. Ist also genau das gegenteil des Heise-Backends. Der Hauptvorteil: mehr als 3MB in über 500 Dateien gespart. Außerdem läuft das auch auf älteren PHP-Versionen (die aber nicht zu empfehlen sind). Voraussetzung: allow_url_fopen muss aktiviert sein.
Ich schau mal, dass ich das noch verbessern kann, und prüfe auch noch mal die APIs der Dienst-Anbieter.