xzsoftware/wykopsdk

SDK dla Api v2 Wykopu

2.1.0 2020-04-11 16:35 UTC

README

WykopSDK is a PHP Software Development Kit for Wykop, makes it easy to develop PHP apps for Wykop.

  • Simple class to handle requests with reasonable groups.
  • Handle all the magic for you.
  • Fully object oriented!
  • Throws exceptions on issues.
  • Abstracts away the API, allowing you to write proper application code.
use XzSoftware\WykopSDK\Exceptions\ApiException;
use XzSoftware\WykopSDK\Profile\Request\Profile;
use XzSoftware\WykopSDK\SDK;
use XzSoftware\WykopSDK\UserManagement\Request\Login;

$sdk = SDK::buildNewSDK('APP_KEY','APP_SECRET');
try {
    $loginResponse = $sdk->auth()->logIn(new Login('USER_NAME', 'USER_ACCOUNT_KEY'));
    $sdk->auth()->authUser('micke', $loginResponse->getUserKey());
    $sdk->profiles()->getProfile(new Profile('micke'));
} catch (ApiException $e) {
    echo $e->getMessage();
    die();
}

Installing

The recommended way to install is through Composer.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version:

php composer.phar require xzsoftware/wykopsdk

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

You can then later update wykopSDK using composer:

php composer.phar update

Usage

Starting point is allways to create new SDK instance. It requires yours Wykop App Key and Secret -> you can get them here.

You can also create an new app. Remember to check all needed permissions

$sdk = XzSoftware\WykopSDK\SDK\SDK::buildNewSDK('APP_KEY','APP_SECRET');

Auth

If you want to do anything you need to start from user authentication. First of all user needs to connect via application connect link.

$sdk = XzSoftware\WykopSDK\SDK\SDK::buildNewSDK('APP_KEY','APP_SECRET');
$sdk->auth()->getConnectLink('127.0.0.1/redirectUrl'); // will return url that yuser needs to follow to access your app. He will be then redirected to redirectUrl param.

// on redirect page you need to read connect data
$data = $sdk->auth()->getAuthFromConnectData(
    $_GET['connectData']
);

// now you can authenticate user with this data:
$sdk->auth()->authUser($data->getLogin(), $data->getToken())

The other option is to access connection via USER_APP_KEY, that user needs to pass from his settings so this is more 'closed' option for power users

use XzSoftware\WykopSDK\SDK;
use XzSoftware\WykopSDK\UserManagement\Request\Login;

$sdk = SDK::buildNewSDK('APP_KEY','APP_SECRET');
$loginResponse = $sdk->auth()->logIn(new Login('USER_NAME', 'USER_ACCOUNT_KEY'));
$sdk->auth()->authUser('USER_NAME', $loginResponse->getUserKey());

Usage

Any endpoint group you can see on docs is handled by specific sdk group, represented in table below:

Group Handles
$sdk->addLink() AddLink
$sdk->auth() Authentication
$sdk->entries() Entries
$sdk->hits() Hits
$sdk->links() Links
$sdk->myWykop() MyWykop
$sdk->notifications() Notifications
$sdk->privateMessages() PM
$sdk->profiles() Profiles
$sdk->search() Search
$sdk->settings() Settings
$sdk->suggest() Suggest
$sdk->tags() Tags

Group can have sub group of endpoint handling specific stuff i.e. $sdk->links()->related()

Endpoint methods usualy accept specific request object as parameter that is needed to handle api call.

$sdk
    ->links()
    ->related()
    ->add(
        new \XzSoftware\WykopSDK\Links\Request\Related\Add(
            12345,
            'http://some.valid.url',
            'some title'
        )
    );

Few endpoints break that rule accepting one of few objects extending specific abstract (voting),

$sdk->links()->related()->vote(
        new \XzSoftware\WykopSDK\Links\Request\Related\VoteUp(1234, 12345)
    );
$sdk->links()->related()->vote(
        new \XzSoftware\WykopSDK\Links\Request\Related\VoteDown(1234, 12345)
    );

or accept simple value like string or int (when no other param is possible and that one is mandatory).

$sdk->privateMessages()->getConversation('username');

All request objects can accept needed params via constructor. Non mandatory params can be modified via setters. i.e

$followersRequest = new \XzSoftware\WykopSDK\Profile\Request\Followers('user');
$followersRequest->setPage(3);
$sdk->profiles()->getFollowers($followersRequest);

IMPORTANT

Each request object has public methods:

    $request->setUserKey('key');
    $request->setAppKey('key');

There is no need to set these two params. SDK client will handle it for you!