PHP code example of audienceplayer / audienceplayer-api-client-php

1. Go to this page and download the library: Download audienceplayer/audienceplayer-api-client-php 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/ */

    

audienceplayer / audienceplayer-api-client-php example snippets


    

    $clientId = 'yourOAuthClientId';
    $clientSecret = 'yourOAuthClientSecret';
    $projectId = 123456;
    $apiBaseUrl = 'https://api.example.com';

    # Instantiate with your clientId, clientSecret, projectId and apiBaseUrl
    $apiClient = AudiencePlayerApiClient::init(
        $clientId,
        $clientSecret,
        $projectId,
        $apiBaseUrl
    );
    
    # Pre-existing bearerToken that you may have cached for given user    
    $bearerToken = 'eYarTl93Fas3...';
    # User-ID or E-mail address of the user for whom you are validating/creating a token
    $userEmail = '[email protected]';
    # When validating with only an e-mail address, the user may be auto-registered if non-existent
    $isAutoRegister = true;
 
    # Compare new token with pre-existing token and update your stored token if necessary 
    $newBearerToken = $apiClient->hydrateValidatedOrRenewedBearerTokenForUser(
        $userId,
        $userEmail,
        $userArgs,
        $bearerToken,
        $isAutoRegister
    );

    # Example of fetching an article with ID=123, and requesting properties name and type
    $result = $apiClient->query
        ->Article(123)                                              # ient->query
        ->ArticleList()
        ->paginate(25, 0)                                           # with limit 25 and offset 0
        ->arguments(['category_id' => 456])                         # fetched all articles part of category with id 456
        ->properties(['id', 'name', 'metas' => ['key', 'value']])   # explicitly ask for given properties
        ->sort(                                                     # sort results by id in descending order
            'id',
            $apiClient:GRAPHQL_OPERATION_SORT_DIRECTION_DESC
        )
        ->execute();

    # Example of searching for specific articles
    $result = $apiClient->query
        ->ArticleList()
        ->search('foobar')                                          # search for articles with matching metadata
        ->properties(['id', 'name', 'metas{key,value}'])            # explicitly ask for given properties
        ->sort(                                                     # sort results by name in descending order
            'name',
            $apiClient:GRAPHQL_OPERATION_SORT_DIRECTION_ASC
        )
        ->execute();

    # Example of updating user details
    $result = $apiClient->mutation
        ->UserDetails()
        ->arguments(['email' => '[email protected]'])               # update with given arguments
        ->execute();

    # Inspect the result
    $result->isSuccessful();                        # result was successfully retrieved and does not contain any errors
    $result->hasErrors();                           # result contains errors
    $result->getErrors();                           # obtain a list of errors

    # Access result properties
    $result->data;                                  # the returned data
    $result->getData();
    
    # Inspect the last graphql operation, which may be helpful for debugging purposes
    $result->getOperationQuery();                   # the executed raw graphQL query, e.g. "query{UserDetails{id,email}}"
    $result->getOperationVariables();               # the used graphQL variables (if applicable), e.g. "{id:1}"

    $operation = 'query Article($articleId:Int) {
                  CustomArticleQuery: Article (id:$articleId) {id name}
              }
    ';

    $result = $apiClient->executeRawGraphQLCall(
        Globals::OAUTH_SCOPE_USER,                  # the oauth-scope you wish to address the api with
        $operation,                                 # raw GraphQL operation (query/mutation) 
        ['articleId' => 1],                         # variables for the operation (if necessary)
        true,                                       # execute as POST request (false for GET)
        true,                                       # retrieve result as parsed object (false for raw JSON)
        'Article',                                  # operation name
        $bearerToken                                # your stored/saved OAuth bearer token
    );

    $result = $apiClient->executeRawGraphQLCall(
        Globals::OAUTH_SCOPE_USER,                  # The oauth-scope you wish to address the api with
        'query{Article(id:1){id,name}}',            # simple raw GraphQL operation without variables 
        [],                                         # the variables for the operation (if necessary)
        true,                                       # execute as POST request (false for GET)
        true,                                       # retrieve result as parsed object (false for raw JSON)
        'Article',                                  # operation name
        $bearerToken                                # your stored/saved OAuth bearer token
    );
bash
    $ composer