PHP code example of questpass / questpass-php-sdk

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

    

questpass / questpass-php-sdk example snippets


use Questpass\SDK\Content;
use Questpass\SDK\InMemoryStorage;
use Questpass\SDK\CurlHttpClient;
use Questpass\SDK\PositioningSettings;

$questpass = new Content(
    # Questpass API endpoint that provides latest javascript
    'https://api.questpass.com/v1/publishers/services/',
    # Unique Service identifier
    'Paste Service UUID here',
    # Implementation that will hold javascript file contents
    new InMemoryStorage,
    new CurlHttpClient,
    PositioningSettings::factory(PositioningSettings::STRATEGY_UPPER)
);

use Questpass\SDK\ElementsContextProvider;

try {
    $javascript = $questpass->javascript([
        $elementsProvider = new ElementsContextProvider,
        new \Questpass\SDK\SubscriptionsContextProvider,
    ]);
} catch (Questpass\SDK\NetworkErrorException $e) {
    // Handle exception here
}

$mainQuestElementId = $elementsProviders->mainQuestId();
$reminderQuestElementId = $elementsProviders->reminderQuestId();

$preparedContent = $questpass->autoPrepare(
    $htmlContent,
    $adContainerHtml,
    $reminderAdContainerHtml
);

$preparedContent = $questpass->manualPrepare(
    $htmlContent,
    $adContainerHtml,
    $reminderAdContainerHtml
);

$hasQuesto = $questpass->hasQuestoInContent($content);

$preparedContent->setJavaScript($javascript);

$subscriberStorage = new SubscriberSessionStorage;
$subscriber = $subscriberStorage->get();
$daysLeft = $subscriber->valid_to->diff(new \DateTime)->days;

new SubscriptionsContextProvider(array(
    SubscriptionsContextProvider::IS_SUBSCRIPTION_ACTIVE => (int)$subscriber->isSubscriptionValid(),
    SubscriptionsContextProvider::IS_SUBSCRIPTION_RECURRING => (int)$isSubscriptionRecurring,
    SubscriptionsContextProvider::IS_SUBSCRIPTION_DAYS_LEFT => $daysLeft,
    SubscriptionsContextProvider::IS_SUBSCRIPTION_AVAILABLE => true,
    SubscriptionsContextProvider::AUTHORIZATION_ERROR => (string)$authorizationError,
    SubscriptionsContextProvider::IS_LOGGED_IN => $subscriber !== null,
    SubscriptionsContextProvider::AUTHORIZATION_URI => $authorizationUri,
    SubscriptionsContextProvider::LOGOUT_URI => $logoutUri,
    SubscriptionsContextProvider::USER_LOGIN => $subscriber->email,
));

use Questpass\SDK\OAuth2Client;
use Questpass\SDK\CurlHttpClient;

$oauth2Client = new OAuth2Client(
    new CurlHttpClient,
    'client_id_here',
    'http://questpass.com/subscriber',
    'http://api.questpass.com/oauth2/token',
    'http://api.questpass.com/oauth2/me',
    'your_redirect_uri'
);

$authorizationUrl = $oauth2Client->authorizationUrl();

$subscriberManager = new SubscriberManager($oauth2Client, new SubscriberSessionStorage);
$subsciber = $subscriberManager->handleRedirect($_GET['code']);

$serviceApiUrl = new \Questpass\SDK\ServiceAPIUrl(
    'https://api.questpass.com/v1/publishers/services/',
    'Paste Service UUID here'
);
$service = new \Questpass\SDK\Service(
    $serviceApiUrl,
    new CurlHttpClient
);
$serviceStatusResponse = $service->fetchStatus();

$javascript = $questpass->requestJavascript();
if ($javascript) {
    $content->getStorage()->set($javascript);
}