PHP code example of melbahja / seo

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

    

melbahja / seo example snippets


use Melbahja\Seo\Schema;
use Melbahja\Seo\Schema\Thing;
use Melbahja\Seo\Schema\Organization;

$schema = new Schema(
    new Organization([
        'url'          => 'https://example.com',
        'logo'         => 'https://example.com/logo.png',
        'contactPoint' => new Thing(type: 'ContactPoint', props: [
            'telephone' => '+1-000-555-1212',
            'contactType' => 'customer service'
        ])
    ])
);

echo $schema;

use Melbahja\Seo\Schema;
use Melbahja\Seo\Schema\Thing;
use Melbahja\Seo\Schema\CreativeWork\WebPage;

$product = new Thing(type: 'Product');
$product->name  = "Foo Bar";
$product->sku   = "sk12";
$product->image = "/image.jpeg";
$product->description = "testing";
$product->offers = new Thing(type: 'Offer', props: [
    'availability' => 'https://schema.org/InStock',
    'priceCurrency' => 'USD',
    "price" => "119.99",
    'url' => 'https://gool.com',
]);

$webpage = new WebPage([
    '@id' => "https://example.com/product/#webpage",
    'url' => "https://example.com/product",
    'name' => 'Foo Bar',
]);


$schema = new Schema(
    $product,
    $webpage,
);

echo json_encode($schema, JSON_PRETTY_PRINT);

use Melbahja\Seo\MetaTags;

$metatags = new MetaTags();

$metatags
        ->title('PHP SEO')
        ->description('This is my description')
        ->meta('author', 'Mohamed Elbahja')
        ->image('https://avatars3.githubusercontent.com/u/8259014')
        ->mobile('https://m.example.com')
        ->canonical('https://example.com')
        ->shortlink('https://git.io/phpseo')
        ->amp('https://apm.example.com')
        ->robots(['index', 'follow', 'max-snippet' => -1])
        ->robots(botName: 'bingbot', options: ['index', 'nofollow'])
        ->feed("https://example.com/feed.rss")
        ->verification("google", "token_value")
        ->verification("yandex", "token_value")
        ->hreflang("de", "https://de.example.com")
        ->og("type", "website")
        ->twitter("creator", "Mohamed Elbahja");
        // ->schema($schema)

echo $metatags;


use Melbahja\Seo\Sitemap;

$sitemap = new Sitemap(
    baseUrl: 'https://example.com',
    saveDir: '/path/to_save/files',
);

$sitemap->links('blog.xml', function($map)
{
    $map->loc('/blog')
            ->changeFreq('daily')
            ->priority(0.8)
            ->loc('/blog/my-new-article')
            ->changeFreq('weekly')
            ->lastMod('2024-01-15')
            ->loc('/اهلا-بالعالم')
            ->changeFreq('weekly');

    $map->loc('/blog/hello')->changeFreq('monthly');
});

$sitemap->render();

$builder->loc('/page')               // URL path relative or absolute
        ->priority(0.8)              // Priority 0.0-1.0
        ->changeFreq('weekly')       // always, hourly, daily, weekly, monthly, yearly, never
        ->lastMod('2024-01-15')      // Last modified date in string or unix ts
        ->image('/image.jpg')        // Add image (

$sitemap->links(['name' => 'gallery.xml', 'images' => true], function($builder)
{
    $builder->loc('/gallery/1')
            ->image('/images/photo1.jpg', [
                'title' => 'Photo Title',
                'caption' => 'Photo caption'
            ]);
});

$sitemap->links(['name' => 'videos.xml', 'videos' => true], function($builder)
{
    $builder->loc('/video/page')
            ->video('Video Title', [
                'thumbnail' => '/thumb.jpg',
                'description' => 'Video description',
                'content_loc' => '/video.mp4'
            ]);
});

use Melbahja\Seo\Sitemap\NewsBuilder;

$sitemap->news('news.xml', function(NewsBuilder $builder)
{
    $builder->setPublication('Your News', 'en');

    $builder->loc('/article/1')
            ->news([
                'title' => 'Article Title',
                'publication_date' => '2024-01-15T10:00:00Z',
                'keywords' => 'news, breaking'
            ]);
});

$sitemap->links(['name' => 'multilang.xml', 'localized' => true], function($builder)
{
    $builder->loc('/page')
            ->alternate('/es/page', 'es')
            ->alternate('/fr/page', 'fr');
});

$sitemap = new Sitemap('https://example.com',
[
    'saveDir' => './storage',
    'mode' => OutputMode::TEMP
]);
$sitemap->render(); // Saves to temp dir and save to disk only on generation success.

$sitemap = new Sitemap('https://example.com',
[
    'saveDir' => './storage',
    'mode' => OutputMode::FILE
]);
$sitemap->render(); // Saves to disk

$sitemap = new Sitemap('https://example.com', [
    'mode' => OutputMode::MEMORY
]);
$xml = $sitemap->render(); // Returns XML string

$stream = fopen('sitemap.xml', 'w');
$builder = new LinksBuilder(
    baseUrl: 'https://example.com',
    stream: $stream, // defaults to stdout
    mode: OutputMode::STREAM,
);
$builder->loc('/page')->render();
fclose($stream);

$sitemap = new Sitemap(baseUrl: 'https://example.com', options: [
    'saveDir' => './sitemaps',
    'indexName' => 'sitemap-index.xml'
]);

// Regular pages y can just pass array of links
$sitemap->links('pages.xml', ['/', '/about', '/contact']);

// Products with images
$sitemap->links(['name' => 'products.xml', 'images' => true], function($builder)
{
    $builder->loc('/product/123')
            ->priority(0.9)
            ->image('/product-main.jpg', ['title' => 'Product Image']);
});

// News section
$sitemap->news('news.xml', function($builder)
{
    $builder->setPublication('Tech News', 'en');
    $builder->loc('/article/1')
            ->news(['title' => 'New Article', 'publication_date' => date('c')]);
});

// Generate everything
$sitemap->render();
// Creates: sitemap-index.xml, pages.xml, products.xml, news.xml

use Melbahja\Seo\Indexing\GoogleIndexer;
use Melbahja\Seo\Indexing\URLIndexingType;

$indexer = new GoogleIndexer('your-google-access-token');

// Index single URL
$indexer->submitUrl('https://www.example.com/page');

// Index multiple URLs
$indexer->submitUrls([
    'https://www.example.com/page1',
    'https://www.example.com/page2'
]);

// Delete URL from index
$indexer->submitUrl('https://www.example.com/deleted-page', URLIndexingType::DELETE);

use Melbahja\Seo\Indexing\IndexNowIndexer;

$indexer = new IndexNowIndexer('your-indexnow-api-key');

// Submit to all supported engines
$indexer->submitUrl('https://www.example.com/page');

// Submit multiple URLs
$indexer->submitUrls([
    'https://www.example.com/page1',
    'https://www.example.com/page2'
]);