1. Go to this page and download the library: Download ampproject/amp-toolbox 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/ */
ampproject / amp-toolbox example snippets
use AmpProject\Optimizer\ErrorCollection;
use AmpProject\Optimizer\TransformationEngine;
$transformationEngine = new TransformationEngine(); // 1.
$errorCollection = new ErrorCollection; // 2.
$optimizedHtml = $transformationEngine->optimizeHtml( // 3.
$unoptimizedHtml, // 4.
$errorCollection // 5.
);
use AmpProject\Dom\Document;
use AmpProject\Optimizer\ErrorCollection;
use AmpProject\Optimizer\TransformationEngine;
if (! $dom instanceof Document) {
$dom = Document::fromNode($dom);
}
$transformationEngine = new TransformationEngine();
$errorCollection = new ErrorCollection;
$transformationEngine->optimizeDom($dom, $errorCollection);
$errorCollection = new ErrorCollection;
// Do the transformation here, while passing in the $errorCollection object.
foreach ($errorCollection as $error) {
printf(
"Error code: %s\nError Message: %s\n",
$error->getCode(),
$error->getMessage()
);
}
if ($errorCollection->count() > 0) {
$this->log('The AMP serverside optimization process produced one or more errors.');
}
if ($errorCollection->has('CannotRemoveBoilerplate')) {
$this->log('The boilerplate was not removed by the Optimizer.');
}
use AmpProject\Optimizer\Configuration;
use AmpProject\Optimizer\DefaultConfiguration;
use AmpProject\Optimizer\TransformationEngine;
use AmpProject\Optimizer\Transformer;
$configurationData = [
Configuration::KEY_TRANSFORMERS => [
Transformer\ServerSideRendering::class,
Transformer\AmpRuntimeCss::class,
Transformer\TransformedIdentifier::class,
],
];
$transformationEngine = new TransformationEngine(
new DefaultConfiguration($configurationData)
);
use AmpProject\Optimizer\Configuration;
use AmpProject\Optimizer\DefaultConfiguration;
use AmpProject\Optimizer\TransformationEngine;
use AmpProject\Optimizer\Transformer;
$configurationData = [
Transformer\AmpRuntimeCss::class => [
Configuration\AmpRuntimeCssConfiguration::CANARY => true,
],
];
$transformationEngine = new TransformationEngine(
new DefaultConfiguration($configurationData)
);
public function transform(Document $document, ErrorCollection $errors)
{
// Apply transformations to the provided $document and ...
// ... add any encountered errors to the $errors collection.
}
use AmpProject\Optimizer\Configuration;
use AmpProject\Optimizer\DefaultConfiguration;
use AmpProject\Optimizer\TransformationEngine;
use MyProject\MyCustomTransformer;
$configurationData = [
Configuration::KEY_TRANSFORMERS => array_merge(
Configuration::DEFAULT_TRANSFORMERS,
[
MyCustomTransformer::class
],
),
];
$transformationEngine = new TransformationEngine(
new DefaultConfiguration($configurationData)
);
use AmpProject\Optimizer\Configuration;
use AmpProject\Optimizer\DefaultConfiguration;
use AmpProject\Optimizer\TransformationEngine;
use MyProject\MyCustomTransformer;
use MyProject\MyCustomTransformerConfiguration;
$configurationData = [
Configuration::KEY_TRANSFORMERS => array_merge(
Configuration::DEFAULT_TRANSFORMERS,
[
MyCustomTransformer::class
],
),
MyCustomTransformer::class => [
MyCustomTransformerConfiguration::SOME_CONFIG_KEY => 'some value',
],
];
$configuration = new DefaultConfiguration($configurationData);
$configuration->registerConfigurationClass(
MyCustomTransformer::class,
MyCustomTransformerConfiguration::class
);
$transformationEngine = new TransformationEngine($configuration);
namespace MyProject;
use AmpProject\Optimizer\Configuration\BaseTransformerConfiguration;
final class MyCustomTransformerConfiguration extends BaseTransformerConfiguration
{
const SOME_CONFIG_KEY = 'some_config_key';
protected function getAllowedKeys()
{
return [
self::SOME_CONFIG_KEY => 'default value',
];
}
protected function validate($key, $value)
{
switch ($key) {
case self::SOME_CONFIG_KEY:
// Validate configuration value here.
}
return $value;
}
}
namespace MyProject;
use AmpProject\Dom\Document;
use AmpProject\Optimizer\Configurable;
use AmpProject\Optimizer\ErrorCollection;
use AmpProject\Optimizer\TransformerConfiguration;
use AmpProject\Optimizer\Transformer;
final class MyCustomTransformer implements Transformer
{
private $configuration;
public function __construct(TransformerConfiguration $configuration)
{
$this->configuration = $configuration;
}
public function transform(Document $document, ErrorCollection $errors)
{
// Bogus transformer logic that adds the configuration value as a body attribute.
$document->body->setAttribute(
'data-my-custom-transformer-body-attribute,
$this->configuration->get(
MyCustomTransformerConfiguration::SOME_CONFIG_KEY
)
);
}
}
namespace MyProject;
use AmpProject\Dom\Document;
use AmpProject\RemoteGetRequest;
use AmpProject\Optimizer\ErrorCollection;
use AmpProject\Optimizer\Transformer;
use Throwable;
final class MyCustomTransformer implements Transformer
{
const END_POINT = 'https://example.com/some_endpoint/';
private $remoteRequest;
public function __construct(RemoteGetRequest $remoteRequest)
{
$this->remoteRequest = $remoteRequest;
}
public function transform(Document $document, ErrorCollection $errors)
{
try {
$response = $this->remoteRequest->get(self::END_POINT);
} catch (Throwable $exception) {
// Add error handling here.
}
$statusCode = $response->getStatusCode();
if (200 < $statusCode || $statusCode >= 300) {
// Add error handling here.
}
$content = $response->getBody();
// Make use of the $content you've just retrieved from an external source.
}
}
use AmpProject\Optimizer\DefaultConfiguration;
use AmpProject\Optimizer\TransformationEngine;
$transformationEngine = new TransformationEngine(
new DefaultConfiguration(),
// A custom implementation that lets you control how remote requests are handled.
new MyCustomRemoteGetRequestImplementation()
);
use AmpProject\Optimizer\DefaultConfiguration;
use AmpProject\Optimizer\TransformationEngine;
use AmpProject\RemoteRequest\CurlRemoteGetRequest;
use AmpProject\RemoteRequest\FallbackRemoteGetRequest;
use AmpProject\RemoteRequest\FilesystemRemoteGetRequest;
const FALLBACK_MAPPING = [
'https://example.com/some_endpoint/' => __DIR__ . '/../fallback_files/some_endpoint.json',
];
$remoteRequest = new FallbackRemoteGetRequest(
new CurlRemoteGetRequest(true, 5, 0), // 5 second timeout with no retries, and ...
new FilesystemRemoteGetRequest(self::FALLBACK_MAPPING) // ... fall back to shipped files.
);
$transformationEngine = new TransformationEngine(new DefaultConfiguration(), $remoteRequest);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.