PHP code example of arielmagbanua / webflow-php-sdk

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

    

arielmagbanua / webflow-php-sdk example snippets


// include the vendor autoload
pi\DataApi\Versions\V2\Authentication\OAuth;
use Dotenv\Dotenv;

// load environment variable if API credentials were configured in the environment
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->safeLoad();

// API credentials
$clientId = $_ENV['WEBFLOW_CLIENT_ID'] ?? null;
$clientSecret = $_ENV['WEBFLOW_CLIENT_SECRET'] ?? null;
$state = $_ENV['WEBFLOW_DEFAULT_STATE'] ?? null;
$redirectUri = $_ENV['WEBFLOW_REDIRECT_URI'] ?? null;

// create OAuth object
$oauth = new OAuth(
    clientId: $clientId,
    clientSecret: $clientSecret,
    state: $state,
    redirectUri: $redirectUri,
    scopes: [
        'cms:read',
        'cms:write',
        'authorized_user:read',
    ]
);

// generate authorization url use the URL to authenticate the client
$authorizationUrl = $oauth->getAuthorizationUrl();

// exchange the authorization code with access token
$tokenObject = $oauth->requestAccessToken($code);

// get the access token value
$accessToken = $tokenObject->getAccessToken();

// include the vendor autoload
pi\DataApi\Versions\V2\Cms\CollectionItems\LiveItems;
use ArielMagbanua\PhpWebflowApi\DataApi\Versions\V2\Cms\CollectionItems\StagedItems;
use ArielMagbanua\PhpWebflowApi\DataApi\Versions\V2\Token\Info;

// retrieved via OAuth or this can be a provided workspace or site access token
$accessToken = 'access_token';

$liveItems = new LiveItems(
    accessToken: $accessToken,
    collectionId: 'live_collection_id' // use the correct collection id from CMS
);
$stagedItems = new StagedItems(
    accessToken: $accessToken,
    collectionId: 'staged_collection_id'
);
$tokenInfo = new Info(
    accessToken: $accessToken
);

// information about the Authorized User
$tokenInfo->getAuthorizationUserInfo();

// information about the authorization token
$tokenInfo->getAuthorizationInfo();

// retrieve live collection items
$liveItems = $liveItems->listItems();

// retrieve staged collection items
$stagedItems = $stagedItems->listItems();