How do you get SEO working when you get tricked in to making an SPA? Magic.
$ composer require roberthucks\seo-magic
$ npm install @nesk/puphpeteer
<?php
include 'vendor/autoload.php';
use roberthucks\SEOMagic\SEOMagic;
$magic = new SEOMagic();
Need to change some of the default settings?
<?php
include 'vendor/autoload.php';
use roberthucks\SEOMagic\SEOMagic;
use roberthucks\SEOMagic\Configuration;
//Change the cache mechanism to disable the cache
$configuration = Configuration::getInstance();
$configuration->cache = roberthucks\SEOMagic\Cacher\NullCache::class;
$magic = new SEOMagic();
fetchPage()
will return a PageResponse
object containing the HTML content of the requested URI.
Pages are cached for 24 hours by default, but this is customisable through the variable redis_cache_default_ttl
.
Setting $fresh
to true will force a fresh copy of the page. Beware though, this isn't the fastest thing in the world.
$magic = new SEOMagic();
$magic->fetchPage('https://example.com');
cachePage()
will store the requested URI in the cache. This is useful for preemptively storing the page in the cache to speed up results of legitimate page requests.
$magic = new SEOMagic();
$magic->cachePage('https://example.com');
This is the object that will be returned from the fetchPage()
function and is used internally to store the page response.
This will return a string that contains the HTML content of the page (without scripts or style tags). This is the bread and butter of the response.
$magic = new SEOMagic();
$magic->fetchPage('https://example.com');
$magic->getHtml(); //<h1>Hello</h1>
This will return an array of all of the headers.
$magic = new SEOMagic();
$magic->fetchPage('https://example.com');
$magic->getHeaders(); //['content-type' => 'text/html']
This will return the response code of the request.
$magic = new SEOMagic();
$magic->fetchPage('https://example.com');
$magic->getResponseCode(); //200
This returns a float that represents the time it took to render the page. This is mainly for performance metrics.
$magic = new SEOMagic();
$magic->fetchPage('https://example.com');
$magic->getRenderTime(); //1.56423
This returns a boolean that states whether or not the page is being fetched from the cache or not. This can be used to check whether your caching system is working correctly and how many hits/misses you are getting.
$magic = new SEOMagic();
$magic->fetchPage('https://example.com');
$magic->isFromCache(); //true