How to fetch Gmail atom feed using PHP simplexml function

I’m trying to build a PHP application that checks how many unread emails I have in my Gmail account. I want to use the Gmail Atom feed for this but I keep running into issues when trying to load the XML data.

Here’s what I’m attempting:

$gmail_feed_url = "https://myemail:[email protected]/gmail/feed/atom/";
$feed_data = simplexml_load_file($gmail_feed_url);
if($feed_data) {
    $total_unread = $feed_data->fullcount;
    echo "Unread emails: " . $total_unread;
}

But I get these errors: “failed to load external entity” and “HTTP request failed! HTTP/1.0 401 Unauthorized”. The stream won’t open properly.

What am I doing wrong here? Is there a better way to authenticate or load this feed? Any help would be great, thanks!

That 401 error means Google ditched basic auth for security reasons - they don’t accept it anymore.

Now you’re stuck with OAuth2, which is a nightmare to set up. You’ve got to register an app, grab credentials, handle token refreshes, and deal with all their API nonsense.

I hit this same wall last year building an email monitoring system. Instead of fighting Google’s OAuth mess and babysitting authentication code, I automated it.

I set up a workflow that connects to Gmail with OAuth, grabs unread counts, and pushes the data to my PHP app through a webhook. Takes maybe 10 minutes to configure and you’re done - no more worrying about expired tokens or broken auth.

The automation deals with Google’s API headaches while your PHP code just gets clean data. Much better than cramming authentication logic into your app.

Check it out here: https://latenode.com

Gmail’s Atom feed is dead - Google killed it for security reasons and now wants OAuth2. But there’s an easier way if you just need unread counts. I hit this same issue building a dashboard. Skip the OAuth mess and just turn on IMAP in Gmail settings, then use PHP’s imap_open() with an app password. Way more reliable and you don’t have to babysit tokens. Generate an app password in your Google account, connect through IMAP, and check message counts. It’s straightforward and won’t break when Google changes things again. Perfect for monitoring unread emails without the auth nightmare.

yo, u gotta fix ur auth! try using file_get_contents with stream_context_create for HTTP auth first, then parse with simplexml_load_string. also, make sure less secure apps is on in your Gmail settings. gl!

Your authentication method is completely outdated. Google killed password auth for their feeds years ago - that’s why you’re getting 401 errors. Hit the same issue migrating an old monitoring script that worked fine until 2019.

I tried switching to cURL with proper context options instead of using simplexml_load_file directly. You can create a stream context with auth headers, but you’ll probably hit the same wall since Google requires OAuth2 now.

Honestly, just use Gmail’s REST API if you’re counting unread emails. Yeah, OAuth setup sucks initially, but once it’s configured it’s way more stable. The Gmail API won’t randomly break when Google updates security policies. Just call users/me/messages with the UNREAD label filter and you’re done.

had the same issue a while back. google’s changed things, so the atom feed is pretty much gone. i switched to using smtp with imap_status() instead. way less hassle than OAuth and works fine for checking unread emails.