ScreenshotNeo

BlogHow-to

How to Add Open Graph Tags to a PHP Website Hosted in India

Add correct Open Graph tags to a PHP site, escape page-specific values, and check that social crawlers can fetch your page and image.

By the ScreenshotNeo team4 October 20266 min read

To add Open Graph tags to a PHP website hosted in India, emit them in the HTML document’s <head> for each page. The four required properties are og:title, og:type, og:image, and og:url. The host’s location does not change the markup; it matters only if the host, CDN, firewall, or access rules prevent social crawlers from fetching the page or image.

Use an absolute canonical URL for og:url, a publicly accessible absolute image URL for og:image, and page-specific title and description values. Escape dynamic values for HTML attribute context. The Open Graph Protocol defines the properties and notes that the first value takes precedence if a property appears more than once, so avoid duplicate or stale tags. See the Open Graph Protocol.

1. Add Open Graph metadata in a PHP template

Prepare metadata from your routing or content layer before including the shared head template. This complete illustrative page shows the required tags and useful additions:

<?php
$pageTitle = 'Example page';
$pageDescription = 'A concise description of this page.';
$canonicalUrl = 'https://example.in/example-page';
$imageUrl = 'https://example.in/images/example-share.jpg';
$escape = static fn (string $value): string => htmlspecialchars(
    $value,
    ENT_QUOTES | ENT_SUBSTITUTE,
    'UTF-8'
);
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?= $escape($pageTitle) ?></title>
    <meta property="og:title" content="<?= $escape($pageTitle) ?>">
    <meta property="og:type" content="website">
    <meta property="og:url" content="<?= $escape($canonicalUrl) ?>">
    <meta property="og:image" content="<?= $escape($imageUrl) ?>">
    <meta property="og:image:alt" content="Share image for <?= $escape($pageTitle) ?>">
    <meta property="og:description" content="<?= $escape($pageDescription) ?>">
    <meta property="og:site_name" content="Example site">
</head>
<body>
    <main>Page content</main>
</body>
</html>

The sample strings and domain are placeholders, not production values. Populate them from trusted application data. In particular, do not build canonical URLs from an unchecked request host: use your site’s configured canonical origin and the current route’s canonical path.

Choose the right type and URL

  • og:type: use a suitable type such as website for a general site page or article for an article.
  • og:url: provide the permanent, absolute URL that identifies this page. Use a consistent canonical URL, including your chosen host and path format.
  • og:image: provide an absolute URL to an image that a crawler can retrieve without a login or browser session.
  • og:image:alt: describe the image for consumers that use its alternative text.

The protocol requires the four core properties; og:description, og:site_name, and image alt text are useful additions. Keep each property deliberate and output it once.

2. Choose static or page-specific metadata

Approach Good fit Watch for
Static values in a PHP template A genuinely single-page site or a page whose preview is the same everywhere. Reusing one title, URL, or image across distinct pages creates misleading shares.
Values supplied by route or content data Multi-page sites, articles, product pages, and other pages needing distinct previews. Provide safe defaults for missing fields and ensure each route resolves to its own canonical URL.

For a shared layout, assemble a metadata structure before rendering the head and pass it to the template. The controller, router, or content lookup should decide the title, description, type, canonical path, and image. The template should escape values and print tags. The exact integration depends on your PHP framework or application structure.

3. Check the rendered response and crawler access

  1. Request a representative page from its public URL.
  2. Inspect the raw HTML response, not just the PHP template or a browser’s modified DOM. Confirm the tags appear inside <head>.
  3. Check that each route has the intended title, type, canonical URL, image, and description, with no duplicate tags.
  4. Open the canonical page and image URLs without being logged in. Confirm the image URL is absolute and reachable by the intended external crawler.
  5. Use a social preview parser to inspect the extracted fields. The Open Graph Protocol page identifies Facebook’s Object Debugger as a parser and debugger.

India does not require a special Open Graph variant. If a crawler cannot fetch a page hosted in India, investigate the actual host, CDN, firewall, bot rules, and access policy. The correct cause depends on that deployment’s configuration.

4. Common problems and fixes

Symptom Likely cause Fix
Preview shows an old or generic title The response has shared static metadata, duplicate properties, or the crawler sees a different response. Inspect raw HTML for the exact URL and remove duplicates. Make values route-specific where needed, then re-fetch with the parser.
Image does not load in the preview The image URL is relative, private, blocked, malformed, or inaccessible to the crawler. Use a complete public HTTPS URL and check that the image can be fetched without cookies or authentication. Check host and CDN rules if access differs by requester.
Tags are missing from page source The template does not print them, metadata was prepared after the head rendered, or output differs by route. Set values before including the head template and inspect the server’s returned HTML for each route.
Special characters break an attribute Dynamic values were printed without HTML attribute escaping. Escape each value with htmlspecialchars(), using ENT_QUOTES | ENT_SUBSTITUTE and the document encoding, as in the example. See the PHP htmlspecialchars manual.
Canonical URL has the wrong domain or path The URL was assembled from an untrusted host header, an inconsistent route, or a noncanonical alias. Build it from trusted canonical site configuration and the route’s intended permanent path.
Template changes seem to have no effect The inspected response may come from application or CDN caching, or the request may reach a different route or deployment. Check the raw response at the exact public URL and inspect the relevant cache and deployment configuration. Do not assume output buffering is needed: the metadata should be assembled before the head is printed. PHP documents buffering and implicit flushing in its output-control configuration manual.

5. Reliability, performance, and maintenance

Open Graph tags are small HTML values; the main reliability concern is serving the right values consistently and keeping referenced pages and images publicly fetchable. Generate metadata from the same route or content record used to render the page, use trusted canonical URL configuration, escape every dynamic attribute value, and define sensible defaults for incomplete content.

Check a sample of important routes after template or routing changes. A parser can show what it extracted, while raw source confirms what the server actually returned. If a parser reports a failure, check access rules and the response for the exact URL; the hosting location alone does not identify the cause. Output buffering is not a requirement for adding these tags when they are emitted in the head at render time.

Or skip the browser setup

Open Graph metadata belongs in PHP’s returned HTML, so a screenshot API does not add or validate these tags. If you also need a rendered screenshot of the page for documentation, review, or an AI workflow, ScreenshotNeo is a website screenshot API and MCP server: one GET request returns a PNG, JPEG, WebP, or PDF. Its capture options include custom CSS and JavaScript, waiting for a selector or network idle, and full-page capture with lazy images loaded. See the ScreenshotNeo API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.in/example-page -o shot.webp

Cookie banners, popups, and chat widgets are removed before the shot. Bot checks, blank pages, and failed loads are never billed, and response headers report the page verdict and billing status. An MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for 1,000 free screenshots a month, with no card required.

FAQ

Does hosting in India change the Open Graph tags?

No. The same protocol properties go in the page head. Hosting and network rules matter only if they affect crawler access.

Do I need to enable PHP output buffering?

No. Prepare values before rendering the shared head template. Buffering is a separate output-control feature.

Can I put a relative path in og:image?

Use an absolute, publicly reachable image URL so a crawler can request it independently of the page’s path.

Should every page have a unique og:url?

Each page should identify itself with its intended permanent canonical URL; do not reuse another page’s URL.