tobiasroeder / open-graph
A compact OpenGraph library
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.94
- symfony/var-dumper: ^8.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-12-03 05:34:11 UTC
README
A compact, type-safe PHP library for generating Open Graph meta tags.
Installation
composer require tobiasroeder/open-graph
Quick Start
use TobiasRoeder\OpenGraph\OpenGraph; $og = new OpenGraph(); $og->setTitle('My Article') ->setDescription('A great article about Open Graph') ->setImage('https://example.com/image.jpg') ->setUrl('https://example.com/article') ->setType('article'); echo $og->render(); // or simply echo $og;
Usage
Basic Properties
$og = new OpenGraph(); // og: namespace properties $og->setTitle('Article Title') ->setDescription('Article description') ->setImage('https://example.com/image.jpg') ->setUrl('https://example.com/page') ->setType('article') ->setSiteName('My Website') ->setLocale('en_US');
Image Properties
The library supports detailed image metadata:
$og->setImage('https://example.com/image.jpg') ->setImageAlt('Image description') ->setImageWidth(1200) ->setImageHeight(630);
Article Properties
For article-specific meta tags:
$og->setAuthor('John Doe') ->setSection('Technology') ->addTag('php') ->addTag('open-graph') ->setPublishedTime(OpenGraph::formatISO8601(new DateTime('2024-06-25 10:00:00'))) ->setModifiedTime(OpenGraph::formatISO8601(new DateTime()));
Multiple Tags
Multiple article:tag values are supported:
// Add tags one by one $og->addTag('php') ->addTag('web-development') ->addTag('meta-tags'); // Or set multiple tags at once $og->setTags('php', 'web-development', 'meta-tags'); // Retrieve tags $tags = $og->getTags(); // Returns array
Formatting Options
With Line Breaks
For formatted HTML output with newlines between tags:
$og->setNewLine(true); echo $og->render();
Output:
<meta property="og:title" content="Article Title" /> <meta property="og:description" content="..." /> <meta property="og:image" content="..." />
Without Line Breaks (default)
echo $og->render();
Output:
<meta property="og:title" content="Article Title" /><meta property="og:description" content="..." /><meta property="og:image" content="..." />
Date Handling
The library enforces strict ISO 8601 date format (YYYY-MM-DDTHH:MM:SSZ) for published and modified times.
Convert DateTime to OpenGraph Format
use TobiasRoeder\OpenGraph\OpenGraph; $date = new DateTime('2024-06-25 10:00:00'); $formatted = OpenGraph::formatISO8601($date); $og->setPublishedTime($formatted);
Valid Date Formats
2024-06-25T10:00:00Z✅ (UTC)
Invalid formats will throw an Exception.
API Reference
Getters
All properties have corresponding getters:
$og->getTitle(): ?string $og->getDescription(): ?string $og->getImage(): ?string $og->getImageAlt(): ?string $og->getImageWidth(): ?int $og->getImageHeight(): ?int $og->getUrl(): ?string $og->getType(): ?string $og->getSiteName(): ?string $og->getLocale(): ?string $og->getAuthor(): ?string $og->getSection(): ?string $og->getPublishedTime(): ?string $og->getModifiedTime(): ?string $og->getTags(): array
Fluent Builder Pattern
All setter methods return the OpenGraph instance for method chaining:
$html = $og->setTitle('Title') ->setDescription('Description') ->setImage('image.jpg') ->render();
Security
The library automatically escapes all content using htmlspecialchars() with ENT_QUOTES to prevent XSS attacks.
$og->setTitle('<script>alert("XSS")</script>'); // Safely rendered as: <script>alert("XSS")</script>
Type Safety
The library uses strict type hints and nullable types:
- String properties:
?string - Integer properties (image dimensions):
?int - Array properties (tags):
array
Invalid types will trigger PHP errors.
License
See LICENSE file.