PHP code example of akse1a / seo

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

    

akse1a / seo example snippets




use Akse1a\Seo\Seo;

$seo = new Seo();

// Set basic meta tags
$seo->setTitle('Page Title')
    ->setDescription('Page description')
    ->setKeywords(['keyword', 'word', 'more']);

// Open Graph
$seo->setImage('https://example.com/image.jpg')
    ->setUrl('https://example.com/page')
    ->setType(\Akse1a\Seo\Enum\OpenGraphType::ARTICLE);

// Output HTML
echo $seo->render();

$seo->addMetaTag('author', 'Author Name')
    ->addMetaTag('robots', 'index, follow');

$seo->addOpenGraph('og:site_name', 'Site Name')
    ->addOpenGraph('og:locale', 'en_US');

$seo->addSchema([
    '@context' => 'https://schema.org',
    '@type' => 'Article',
    'headline' => 'Article Headline',
    'author' => [
        '@type' => 'Person',
        'name' => 'Author Name'
    ]
]);



use Akse1a\Seo\Sitemap;
use Akse1a\Seo\Enum\ChangeFrequency;
use Akse1a\Seo\Exception\RuntimeException;

$sitemap = new Sitemap();

// Add URLs
$sitemap->addUrl('https://example.com/')
    ->addUrl(
        'https://example.com/page',
        '2024-01-15',  // lastmod
        'weekly',      // changefreq
        0.8            // priority
    )
    ->addUrlWithNow('https://example.com/new-page', ChangeFrequency::DAILY, 1.0);

// Generate XML string
$xml = $sitemap->generate();
echo $xml;

// Save to file sitemap.xml
try {
    $sitemap->save('sitemap.xml');
    echo "Sitemap successfully saved to sitemap.xml\n";
} catch (RuntimeException $e) {
    echo "Error saving: " . $e->getMessage() . "\n";
}



use Akse1a\Seo\Sitemap;
use Akse1a\Seo\Enum\ChangeFrequency;
use Akse1a\Seo\Exception\InvalidArgumentException;
use Akse1a\Seo\Exception\RuntimeException;

try {
    $sitemap = new Sitemap();
    
    // Add URLs with different parameters
    $sitemap->addUrl('https://example.com/')
        ->addUrl(
            'https://example.com/about',
            new \DateTime('2024-01-15'),
            ChangeFrequency::MONTHLY,
            0.8
        )
        ->addUrl(
            'https://example.com/blog',
            '2024-01-20',
            ChangeFrequency::WEEKLY,
            0.9
        )
        ->addUrlWithNow('https://example.com/contact', ChangeFrequency::YEARLY, 0.7);
    
    // Generate XML
    $xml = $sitemap->generate();
    
    // Save to file
    $filename = __DIR__ . '/sitemap.xml';
    $sitemap->save($filename);
    
    // Check if file was created
    if (file_exists($filename)) {
        echo sprintf("Sitemap successfully created: %s (%d bytes)\n", $filename, filesize($filename));
    }
    
} catch (InvalidArgumentException $e) {
    echo "Validation error: " . $e->getMessage() . "\n";
} catch (RuntimeException $e) {
    echo "Runtime error: " . $e->getMessage() . "\n";
}



use Akse1a\Seo\Sitemap;

$sitemap = new Sitemap();
$sitemap->addUrl('https://example.com/')
    ->addUrl('https://example.com/page');

// Output with correct HTTP headers
$sitemap->output();
exit;

if ($sitemap->hasUrl('https://example.com/page')) {
    echo "URL already exists in sitemap";
}

use Akse1a\Seo\Exception\InvalidArgumentException;
use Akse1a\Seo\Sitemap;

try {
    $sitemap = new Sitemap();
    $sitemap->addUrl('invalid-url'); // Will throw InvalidArgumentException
} catch (InvalidArgumentException $e) {
    echo $e->getMessage();
}

$sitemap = new Sitemap();

$pages = [
    ['url' => 'https://example.com/', 'priority' => 1.0, 'changefreq' => 'daily'],
    ['url' => 'https://example.com/about', 'priority' => 0.8, 'changefreq' => 'monthly'],
    ['url' => 'https://example.com/contact', 'priority' => 0.7, 'changefreq' => 'monthly'],
];

foreach ($pages as $page) {
    $sitemap->addUrlWithNow($page['url'], $page['changefreq'], $page['priority']);
}

$sitemap->save('sitemap.xml');
bash
# Run sitemap generation example
php example_sitemap.php