Get title of website via link

Get title of website via link

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Website Titles from URLs in PHP

Seeing a list of source links below a news excerpt, like the one Google News displays with "The Guardian - ABC News - Reuters," is a classic use case for metadata enrichment. You want to take an arbitrary URL, such as http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/, and programmatically resolve it to the human-readable name of the site: "The Washington Times".

In PHP, this isn't magic; it's a combination of HTTP requests, header parsing, and heuristic logic. The core challenge lies in distinguishing between the page title (the HTML <title> tag) and the organization name (which is often embedded in meta tags or extracted via scraping).

Using the headers Array for Basic Extraction

The most straightforward approach relies on the HTTP response headers returned by the server. When you fetch a URL, the server typically includes an X-Powered-By header or, more reliably for titles, allows access to the content type and status. However, standard PHP cURL doesn't give you the HTML <title> tag directly via headers.

To get the actual title text, you must retrieve the HTML body and parse it. You can use file_get_contents() combined with a DOM parser or the built-in DOMDocument class. Here is a basic implementation:

$url = 'http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/';
$html = file_get_contents($url);

if ($html === false) {
    die('Could not fetch URL');
}

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_NOERROR | LIBXML_NOWARNING);

$titleNode = $dom->getElementsByTagName('title')->item(0);
echo $titleNode ? $titleNode->nodeValue : 'No title found';

This snippet returns the raw text inside the <title> tag. For many news sites, this string matches the publication name perfectly. For others, it might say "Debt Panel Fails Test Vote" (the article headline) rather than the site name.

Handling Meta Tags and Canonical URLs

Relying solely on the HTML title is brittle. A robust solution often involves checking for specific meta tags that explicitly declare the site name or looking for a canonical URL that points to a homepage containing the logo or name.

You can inspect the meta tag with a property like name="sitename" or property="og:site_name". Open Graph data, in particular, is widely adopted by social platforms and news outlets.

$meta = [];
foreach ($dom->getElementsByTagName('meta') as $node) {
    $attrs = array_filter($node->attributes, fn($a) => in_array($a->name, ['name', 'property']));
    if (!empty($attrs)) {
        foreach ($attrs as $attr) {
            $key = $attr->name;
            $value = $attr->value;
            // Handle property="og:site_name"
            if (strpos($key, 'og:') === 0) {
                $key = substr($key, 4); 
            }
            $meta[$key] = $value;
        }
    }
}

$ogSiteName = isset($meta['og:site_name']) ? $meta['og:site_name'] : null;

This approach adds a layer of reliability. If the server provides og:site_name, you use that. If not, you fall back to the HTML title or the URL's hostname.

The Trade-Offs and Limitations

Building a scraper in raw PHP introduces several pitfalls. First, robots.txt compliance is mandatory. Some sites explicitly block crawlers from accessing their main content or meta tags. Ignoring this can get your IP rate-limited or blocked entirely.

Second, performance matters. Fetching full HTML documents for every link is I/O intensive. If you are processing thousands of URLs, you should consider caching the results in a database or Redis to avoid redundant network calls.

Third, the structure of news sites changes frequently. A meta tag present today might be removed tomorrow. This volatility makes maintaining a static list of extraction rules difficult. In production environments, switching to a dedicated scraping library or leveraging an API like NewsAPI is often more sustainable than rolling your own parser.

If you are building this feature within the Laravel ecosystem, consider checking resources from https://laravelcompany.com for existing packages that handle HTTP requests and HTML parsing with better exception handling and middleware support. They can simplify the boilerplate code required to manage timeouts, user agents, and error logging automatically.

Final Recommendation

Start simple: fetch the HTML, parse the <title> tag, and check for og:site_name. Only move to complex graph scraping or external APIs if you encounter consistent failures with specific domains. Always respect robots.txt and ensure your user agent string identifies your application clearly to avoid triggering anti-bot defenses.

Our network