PHP code example of youthweb / php-youthweb-api

1. Go to this page and download the library: Download youthweb/php-youthweb-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

youthweb / php-youthweb-api example snippets




error_reporting(E_ALL);
ini_set('display_errors', 1);

// Config
$client_id = 'CB91ZullPa4ync4l';
$client_secret = 'YC7CXuDXX9pF5SeTKs9enkoPjbV01QIs';
$redirect_url = 'http://localhost/php-youthweb-api/login-button.php';
$scope = ['user:read']; // See http://developer.youthweb.net/api_general_scopes.html
//A resource owner identifier to separate the caches
$resourceOwnerId = 'a24d4387-f4de-4318-929a-57d475162fd4'; // or '12345' or '[email protected]'

action="'.$redirect_url.'">
<input name="go" value="Login" type="submit" />
</form>';

if ( isset($_GET['go']) )
{
    try {
        // (1) Try access the API
        $me = $client->getResource('users')->showMe();
    } catch (\Youthweb\Api\Exception\UnauthorizedException $th) {
        // (2) We need to ask for permission first
        header('Location: '.$th->getAuthorizationUrl());
        exit;
    }

    // (4) We have access to the API \o/
    printf('<p>Hallo %s %s!</p>', $me->get('data.attributes.first_name'), $me->get('data.attributes.last_name'));
    printf('<p>Deine Email-Adresse: %s', $me->get('data.attributes.email'));
}
elseif ( isset($_GET['code']) )
{
    // (3) Here we are if we have a permission
    $client->authorize('authorization_code', [
        'code' => $_GET['code'],
        'state' => $_GET['state'],
    ]);

    header('Location: '.$redirect_url.'?go=Login');
    exit;
}

$ composer