
Ahrefs MCP server is golden if you ask me, but the documentation only shows how to connect it to an AI web interface such as ChatGPT Plus/Pro or Claude, but that didn’t cut it for me.
I want to acces it in PHP so I can set up a cronjob and go through hundreds of sites instead of manually through a web interface.
Since there is no documentation available for that it took a few hours of playing around.
Eventually I did set up the web interface to test it and it worked and then I asked GPT-5 what it used to make the connection and the truth was revealed.
We need to use tools/call first as the method and then we can define the actual method such as site-explorer-organic-keywords.
Here is the full code (this will print the results on your screen):
<?php
 /*
 Template Name: Ahrefs MCP Server
 */
$mcpUrl = ‘https://api.ahrefs.com/mcp/mcp’;
 $mcpHeaders = [
 ‘Authorization: Bearer YOUR_MCP_API_KEY’,
 ‘Content-Type: application/json’,
 ‘Accept: application/json’
 ];
$requestBody = json_encode([
 ‘jsonrpc’ => ‘2.0’,
 ‘method’ => ‘tools/call’,
 ‘id’ => ‘1’,
 ‘params’ => [
 ‘name’ => ‘site-explorer-organic-keywords’,
 ‘arguments’ => [
 ‘target’ => ‘https://www.serptrust.io’,
 ‘mode’ => ‘prefix’,
 ‘protocol’ => ‘https’,
 ‘date’ => ‘2025-10-30’,
 ‘limit’ => 100,
 ‘order_by’ => ‘volume:desc’,
 ‘select’ => ‘keyword,volume’
 ]
 ]
 ]);
$ch = curl_init($mcpUrl);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $mcpHeaders);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
 // curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
 $response = curl_exec($ch);
if ($response === false) {
 echo “cURL Error: ” . curl_error($ch) . “\n”;
 } else {
 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 echo “HTTP Status: $httpCode\n”;
 if ($httpCode === 200) {
 $result = json_decode($response, true);
 if (json_last_error() === JSON_ERROR_NONE) {
 var_dump($result);
 } else {
 echo “JSON Decode Error: ” . json_last_error_msg() . “\n”;
 }
 } else {
 echo “Raw Response: $response\n”;
 }
 }
 curl_close($ch);
?>
The next thing you can do is use: if ($result) { }
You can put your OpenAI API call within the { } brackets and use $result in the instructions, for example:
Simply put the code in a text file, save it in PHP format, upload it in your theme folder and then load it as a custom page template (eg Add Page, select Template) and then all you got to do is load the page and it runs.
You can connect it to a database table with sites in MySQL to automate it and run it on a cron job.
As you can see in the featured image I build a site evaluation script that analysis the keywords a site ranks for and gives it a total score from 0-10 and shows the commercial value etc, well just check the screenshot.
I’ve been doing this work manually when updating our guest post database and must’ve done it for over a 100,000 sites.
This is why our database is only 500 sites and that truly means something.
