Sign inSign up

heiseonline/shariff-backend-php

By heiseonline

•Updated over 8 years ago

PHP backend for Shariff using php-fpm.

Image
0

50K+

heiseonline/shariff-backend-php repository overview

⁠Shariff PHP Backend Build Status Dependency Status Code Climate

Shariff is used to determine how often a page is shared in social media, but without generating requests from the displaying page to the social sites.

Shariff Logo © 2014 Heise Zeitschriften Verlag

This document describes the PHP backend. The following backends are also available:

⁠Supported services

  • AddThis
  • Facebook
  • Flattr
  • LinkedIn
  • Pinterest
  • Reddit
  • StumbleUpon
  • Xing
  • Vk

⁠Requirements

To run Shariff PHP Backend on your server you need one of these PHP versions:

  • 5.6
  • 7.0
  • 7.1

Older versions and HHVM are not supported.

⁠Installing the Shariff backend on you own server

To run Shariff under a certain URL, unzip the release⁠ zip file into a directory under the document root of your web server.

Take a look into the demo application in the index.php file and adjust the configuration as necessary.

The following configuration options are available:

KeyTypeDescription
cacheClassstringOptional Cache class name. Has to implement Heise\Shariff\CacheInterface. Defaults to internal Zend Cache.
cacheobjectFile cache settings, which are passed on to the Cache class. See description below.
domainsarrayDomains for which share counts may be requested. If empty, all domains are allowed.
servicesarrayList of services to be enabled. See Supported services⁠.
⁠Cache settings:

By default Shariff uses the Filesystem cache. By specifying a different adapter from Zend\Cache\Storage\Adapter you can tell Shariff to use another cache. Also you can specify options for that cache adapter

KeyTypeDescription
ttlintegerTime that the counts are cached (in seconds)
cacheDirstringDirectory used for the cache. Default: system temp directory
adapterstringName of cache adapter (e.g. Apc, Memcache, etc.)
adapterOptionsobjectOptions for the cache adapter

These option apply for the default Cache class (ZendCache) only. If you implement custom caching, you can specify your own options.

⁠Client options

The backend uses Guzzle⁠ as HTTP client. Guzzle has many options that you can set, e.g. timeout and connect_timeout. See http://docs.guzzlephp.org/en/latest/request-options.html⁠ for a detailed list. In order to set those options pass them in the json with the key "client".

KeyTypeDescription
clientobjectGuzzle request options
⁠Service Settings

To pass config options to a service, you can add them to the json as well under the name of the service. Currently only the Facebook service has options for an facebook application id and client secret in order to use the graph api id method to get the current share count.

KeyTypeDescription
servicenameobjectoptions for the service
⁠Facebook service options

To use the graph api id method to fetch the share count you need to set up an application at facebook.com and pass in the application id and client secret to the options. It seems that the id method returns the most current share count, but it can be only used with an registered application.

KeyTypeDescription
app_idstringthe id of your facebook application
secretstringthe client secret of your facebook application
⁠Full config example
/**
 * Sample configuration
 *
 * @var array
 */
private static $configuration = [
    'cacheClass' => 'Heise\\Shariff\\ZendCache',
    'cache' => [
        'ttl' => 60,
        'cacheDir' => '/tmp/shariff/cache',
        'adapter' => 'Filesystem',
        'adapterOptions' => [
          // ...
        ]
    ],
    'client' => [
      'timeout' => 4.2
      // ... (see "Client options")
    ],
    'domains' => [
        'www.heise.de',
        'www.ct.de'
    ],
    'services' => [
        'Facebook',
        'LinkedIn',
        'Reddit',
        'StumbleUpon',
        'Flattr',
        'Pinterest',
        'Xing',
        'AddThis',
        'Vk'
    ],
    'Facebook' => [
      'app_id' => '1234567890',
      'secret' => 'terces'
    ]
];

⁠Testing your installation

If the backend runs under http://example.com/my-shariff-backend/, calling the URL http://example.com/my-shariff-backend/?url=http%3A%2F%2Fwww.example.com should return a JSON structure with numbers in it, e.g.:

{"facebook":1452,"linkedin":118,"reddit":7,"stumbleupon":4325,"flattr":0,"pinterest":3,"addthis":33,"vk":326}

⁠Shariff OO interface

If you need more control, you can invoke Shariff in your own PHP code. The following snippet should get you started. $options are identical to those described above.

use Heise\Shariff\Backend;

$options = [
	"domains"  => ["www.heise.de", "www.ct.de"],
	"cache"    => ["ttl" => 1],
	"services" => ["Facebook", "LinkedIn", "Reddit", "StumbleUpon", "Flattr", "Pinterest", "AddThis", "Vk"]
];
$shariff = new Backend($options);
$counts = $shariff->get("http://www.heise.de/");
echo $counts["facebook"];

Tag summary

Content type

Image

Digest

Size

37.2 MB

Last updated

over 8 years ago

docker pull heiseonline/shariff-backend-php:docker