Problem purging APO created mobile cache via API

I use APO for Wordpress on my site. It’s set to Cache by Device Type.

A few most important pages of the website are custom-made. Made in a way that (unintentionally) prevents APO from recognizing when they change. So APO cannot purge Cloudflare cache for them when they change. That’s all fine and ok.

I purge their Cloudflare cache the moment they change via API using curl.

However, it has come to my attention that this way a Mobile cache doesn’t get purged. Only Desktop one does.

This used to be a general APO issue, reported here:
https://github.com/cloudflare/Cloudflare-WordPress/issues/404
APO itself wasn’t purging mobile caches.

This issue was then fixed, yet the challenge remains for purging APO-created mobile cache via API.

I am guessing here but this is likely because APO uses some extra custom cache keys that I am not aware of. I don’t know of a way to check how APO communicates with Cloudflare and what custom keys or header it sends.

I added “CF-Device-Type: mobile” header to the API request, yet it didn’t do the trick. So there must be something else that needs to be passed to the API for it to purge the mobile cache.

I use this basic PHP code:

<?php
$zoneId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$email = "[email protected]";

$head = [];
$head[] = 'Content-Type: application/json';
$head[] = "X-Auth-Email: $email";
$head[] = "X-Auth-Key: $apiKey";
$head[] = 'CF-Device-Type: mobile';
$head[] = 'cache-control: no-cache';

$url = "https://api.cloudflare.com/client/v4/zones/$zoneId/purge_cache";

$purge = ['files' => ['https://www.domain.com/']];
    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($purge));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
?>

There are 2 solutions:

I need to know what else I need to put inside the header that’s sent to the API or if there’s something else I need to add to the API call in the above PHP code.

I could use the same APO Wordpress plugin code that purges cache via API. I just don’t know where that is stored in the plugin. I am more of a script kiddie than a coder, so I need a little help.

An APO dev could be of great help here. Or anyone who has dealt with this issue before.

It’s not the header of your API call that needs to change. It’s the payload. The $purge files needs to include the device type:

2 Likes

Yes, now it works!

Thank you so much.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.