PageCDN is a Content Delivery Network and Content Optimization Service to accelerate your websites by upto 10x.
- PHP 5.3
- PHP Curl Extension
- PHP JSON Extension
- Write access to cache directory
- Initial Setup
- Using Public CDN
- Using Private CDN
- Using Public Private CDN
- Enabling Optimizations
- Resizing, Converting and Optimizing Images
- Purge and Cache cleanup
- Basic API Calls
To use this library, simply include the pagecdn.php
file as below:
<?php
require 'pagecdn.php';
This library relies heavily on local cache to speed up several CDN operations like conversion of your website URLs to CDN URLs, fetching Public CDN resources, caching API requests, etc. For such purpose and to ease the conversion of URLs, this libraries requires that you specify your Origin URL and provide write access to a cache directory.
<?php
require 'pagecdn.php';
$options = [ 'origin_url' => 'https://your-website.com/blog' , //Always Required
'cache_dir' => './sdk-cache/' //Always required
];
$pagecdn = PageCDN::init( $options );
PageCDN offers both Public CDN and Private CDN. Using Public CDN is FREE and DOES NOT even require an account. Here is how you can link to a resource automatically from Public CDN.
<?php
require 'pagecdn.php';
$options = [ 'public_cdn' => true ,
'origin_url' => 'https://your-website.com/blog' , //Always Required
'cache_dir' => './sdk-cache/' //Always required
];
$pagecdn = PageCDN::init( $options );
echo $pagecdn->url('https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.css?ver=4.3.1');
echo $pagecdn->url('https://your-website.com/blog/wp-includes/js/jquery/jquery.js');
# Result:
# https://pagecdn.io/lib/bootstrap/4.3.1/css/bootstrap.min.css
# https://pagecdn.io/repo/wp-includes/5.4/js/jquery/jquery.js
Using Private CDN requires API Key and optionally CDN URL in addition to the basic requirements set forth in the Initial Setup section above. This library can automatically create a private repo (Zone) for you to ease the CDN connection. Here is example of a basic Private CDN setup.
<?php
require 'pagecdn.php';
$options = [ 'private_cdn'=> true ,
'origin_url' => 'https://your-website.com/blog' , //Always Required
'apikey' => '3e692034d5b4d31326f8dc637229ee6f95a50e1242394420f07a8597934c0cc0' , //Required for Private CDN
'cdn_url' => 'https://pagecdn.io/site/abcxyz' , //Optional. Library can automatically find or create a CDN_URL for you
'cache_dir' => './sdk-cache/' //Always required
];
$pagecdn = PageCDN::init( $options );
echo $pagecdn->url('https://your-website.com/blog/assets/hello.css');
# Result:
# https://pagecdn.io/site/abcxyz/assets/hello.css
It is possible to use Public and Private CDN together. In such a case, this library will automatically find the most optimal way to link to a resource.
<?php
require 'pagecdn.php';
$options = [ 'private_cdn'=> true ,
'public_cdn' => true ,
'origin_url' => 'https://your-website.com/blog' , //Always Required
'apikey' => '3e692034d5b4d31326f8dc637229ee6f95a50e1242394420f07a8597934c0cc0' , //Required for Private CDN
'cdn_url' => 'https://pagecdn.io/site/abcxyz' , //Optional. Library can automatically find or create a CDN_URL for you
'cache_dir' => './sdk-cache/' //Always required
];
$pagecdn = PageCDN::init( $options );
echo $pagecdn->url('https://your-website.com/blog/assets/hello.css');
echo $pagecdn->url('https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.css?ver=4.3.1');
echo $pagecdn->url('https://your-website.com/blog/wp-includes/js/jquery/jquery.js');
# Result:
# https://pagecdn.io/site/abcxyz/assets/hello.css [Private CDN]
# https://pagecdn.io/lib/bootstrap/4.3.1/css/bootstrap.min.css [Public CDN]
# https://pagecdn.io/repo/wp-includes/5.4/js/jquery/jquery.js [Public CDN]
Linking to resources from Public CDN is recommended as it increases cache hit ratio in browser and on edge. Using Public CDN is free, and results in bandwidth cost saving.
All the above examples help with loading resources over CDN. But this is not all what PageCDN can do for you. PageCDN allows you to optimize your website resources aggressively without worrying about maintaining complex optimization tools, configurations and multiple copies of optimized files. Optimizing resources with PageCDN is as simple as just linking to a resource.
<?php
require 'pagecdn.php';
$options = [ 'private_cdn'=> true ,
'public_cdn' => true ,
'origin_url' => 'https://your-website.com/blog' , //Always Required
'apikey' => '3e692034d5b4d31326f8dc637229ee6f95a50e1242394420f07a8597934c0cc0' , //Required for Private CDN
'cdn_url' => 'https://pagecdn.io/site/abcxyz' , //Optional. Library can automatically find or create a CDN_URL for you
'cache_dir' => './sdk-cache/' , //Always required
//Optimizations
'remove_querystring' => true ,
'optimize_images' => true ,
'minify_css' => true ,
'minify_js' => true ,
];
$pagecdn = PageCDN::init( $options );
echo $pagecdn->url('https://your-website.com/blog/assets/hello.js?ver=4.2.1');
echo $pagecdn->url('https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.css?ver=4.3.1');
echo $pagecdn->url('https://your-website.com/blog/assets/company-logo.png');
# Result:
# https://pagecdn.io/site/abcxyz/assets/hello.min.js [Minified JS on Private CDN, Removed Querystring]
# https://pagecdn.io/lib/bootstrap/4.3.1/css/bootstrap.min.css [Minified CSS on Public CDN]
# https://pagecdn.io/site/abcxyz/assets/company-logo._o_webp.png [Optimized and Converted to WebP if browser supports for WebP is available]
All these optimizations are handled by PageCDN on the fly.
Enabling above image optimization in itself can speedup website by a big margin. However, you may need to resize images to smaller dimensions to save unnecessary bandwidth spent on delivering large images. Here is how to achieve that.
<?php
require 'pagecdn.php';
$options = [ 'private_cdn'=> true ,
'public_cdn' => true ,
'origin_url' => 'https://your-website.com/blog' , //Always Required
'apikey' => '3e692034d5b4d31326f8dc637229ee6f95a50e1242394420f07a8597934c0cc0' , //Required for Private CDN
'cdn_url' => 'https://pagecdn.io/site/abcxyz' , //Optional. Library can automatically find or create a CDN_URL for you
'cache_dir' => './sdk-cache/' , //Always required
//Optimizations
'remove_querystring' => true ,
'optimize_images' => true ,
'minify_css' => true ,
'minify_js' => true ,
];
$pagecdn = PageCDN::init( $options );
echo $pagecdn->image('https://your-website.com/blog/assets/company-logo.png',['width'=>300,'height'=>100]);
# Result:
# https://pagecdn.io/site/abcxyz/assets/company-logo._o_300w_100h_webp.png
$pagecdn->purge('https://your-website.com/blog/assets/company-logo.png');
$pagecdn->purge_all( );
$pagecdn->reset_cache( );
The below method allows you to conveniently make API calls. For documentation of API endpoints, visit docs.
$endpoint = '/private/account/repos'; //list all repos in my account
$method = 'get';
$fields = []; //no get or post fields required other than apikey
$cache = true; //cache response locally, can be removed with reset_cache( )
$pagecdn->api_request( $endpoint , $method , $fields , $cache );
Some of the PageCDN features are not yet implemented in this library. Please wait for the future releases to avail those features, or roll your own implementation or library with the help of API Docs.
- In-URL Cache Busters
- Storage Buckets / Push CDN
- Pull CDN for Github Repos
- Autmatic conversion of Google Fonts or other Font URLs to Easyfonts.