PHP code example of bit-part / mt-data-api-php-client
1. Go to this page and download the library: Download bit-part/mt-data-api-php-client 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/ */
bit-part / mt-data-api-php-client example snippets
use Bitpart\MtDataApi\Client;
use GuzzleHttp\Client as GuzzleClient;
use Nyholm\Psr7\Factory\Psr17Factory;
// Initialize the client
$httpClient = new GuzzleClient();
$factory = new Psr17Factory();
$client = new Client(
baseUrl: 'https://example.com/mt-data-api.cgi/v7',
httpClient: $httpClient,
requestFactory: $factory,
streamFactory: $factory
);
// Authenticate (clientId is strongly recommended)
$authenticator = $client->getAuthenticator();
$authResult = $authenticator->login(
username: 'username',
password: 'password',
remember: true,
clientId: 'my-application'
);
$client = $authResult->client;
// Make API calls
$sites = $client->raw('GET', 'sites');
use Bitpart\MtDataApi\Query\Query;
// Entries scoped to site 1
$entries = $client->entries(1);
// List entries (returns the `items` array)
$items = $entries->list(Query::make()->limit(20));
// Create a new entry
$created = $entries->create([
'title' => 'Hello World',
'status' => 'Publish',
]);
// Upload an asset
$asset = $client->assets(1)->upload('/path/to/image.jpg', ['label' => 'Cover']);
// Publish a template
$client->publish(1)->template(123);
// Preview a page
$preview = $client->preview(1)->page([
'page' => ['title' => 'Draft title'],
]);
use Bitpart\MtDataApi\Auth\Authenticator;
$authenticator = new Authenticator($client);
// Login with username/password
// Note: clientId is strongly recommended for MT Data API v7
$login = $authenticator->login(
username: 'username',
password: 'password',
clientId: 'my-application' // Identifies your application
);
$client = $login->client; // holds access token & session id
// Refresh token later using stored session id
$refreshed = (new Authenticator($client))->refresh();
$client = $refreshed->client;
// Logout to invalidate session & token
$client = (new Authenticator($client))->logout();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.