Skip to content

Latest commit

 

History

History
195 lines (150 loc) · 4.18 KB

File metadata and controls

195 lines (150 loc) · 4.18 KB

API Error Fix - Detailed Explanation

The Errors You Were Seeing

Error 1: Browser Extension Error (Not Our Problem)

Unchecked runtime.lastError: The message port closed before a response was received

What it means: This is a Chrome/Edge browser extension error, NOT your code. Some browser extension tried to communicate with the page and failed.

Solution: Ignore it - it's harmless and doesn't affect your app.


Error 2: 404 Not Found (THE REAL PROBLEM)

api.php/api/seo-analysis/analyze:1 Failed to load resource: 
the server responded with a status of 404 (Not Found)

What it means: The API router couldn't find the /api/seo-analysis/analyze endpoint.

Root cause: The router was receiving URLs like:

/rankmath/webapp/api.php/api/seo-analysis/analyze

But it wasn't properly stripping /api.php from the path, so it was looking for:

/api.php/api/seo-analysis/analyze  ❌ WRONG

Instead of:

/api/seo-analysis/analyze  ✅ CORRECT

What I Fixed

1. Router Path Parsing (core/Router.php)

Before:

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$basePath = '/rankmath/webapp';
if (strpos($path, $basePath) === 0) {
    $path = substr($path, strlen($basePath));
}
// Path still had /api.php in it ❌

After:

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

// Remove /rankmath/webapp
$basePath = '/rankmath/webapp';
if (strpos($path, $basePath) === 0) {
    $path = substr($path, strlen($basePath));
}

// Remove /api.php (NEW!)
if (strpos($path, '/api.php') === 0) {
    $path = substr($path, strlen('/api.php'));
}

// Ensure path starts with /
if (empty($path) || $path[0] !== '/') {
    $path = '/' . $path;
}

2. JSON Input Handling (SeoAnalysisController.php)

Before:

$url = $_POST['url'] ?? '';  // Doesn't work with JSON

After:

$input = json_decode(file_get_contents('php://input'), true);
$url = $input['url'] ?? '';  // Now reads JSON properly

How It Works Now

Request Flow:

  1. Browser sends:

    POST /rankmath/webapp/api.php/api/seo-analysis/analyze
    Body: {"url": "https://example.com", "is_competitor": false}
    
  2. Router receives:

    REQUEST_URI: /rankmath/webapp/api.php/api/seo-analysis/analyze
    
  3. Router strips base paths:

    Step 1: Remove /rankmath/webapp
    Result: /api.php/api/seo-analysis/analyze
    
    Step 2: Remove /api.php  
    Result: /api/seo-analysis/analyze  ✅
    
  4. Router matches route:

    POST /api/seo-analysis/analyze  →  SeoAnalysisController@analyze
    
  5. Controller reads JSON:

    $input = json_decode(file_get_contents('php://input'), true);
    // Gets: ["url" => "https://example.com", "is_competitor" => false]
  6. Returns success:

    {
      "success": true,
      "message": "Analysis complete",
      "data": {
        "score": 85,
        "results": {...}
      }
    }

Testing

Quick Test (Open in browser):

http://localhost/rankmath/webapp/test-api.html

This test page lets you:

  • ✓ Test the health endpoint
  • ✓ Test dashboard stats
  • ✓ Test SEO analysis with any URL

Manual API Tests:

Health Check:

curl http://localhost/rankmath/webapp/api.php/api/health

Dashboard Stats:

curl http://localhost/rankmath/webapp/api.php/api/dashboard/stats

SEO Analysis:

curl -X POST http://localhost/rankmath/webapp/api.php/api/seo-analysis/analyze \
  -H "Content-Type: application/json" \
  -d '{"url":"https://example.com","is_competitor":false}'

Why This Happened

The original code worked in a root directory setup like:

/api.php/api/seo-analysis/analyze

But in your subdirectory setup:

/rankmath/webapp/api.php/api/seo-analysis/analyze

The router needed extra logic to strip both the subdirectory AND the api.php filename.


All Fixed! ✅

Your API routing now works correctly for:

  • ✅ Subdirectory installations (/rankmath/webapp/)
  • ✅ JSON POST requests
  • ✅ All HTTP methods (GET, POST, PUT, DELETE)
  • ✅ Dynamic route parameters ({id})
  • ✅ Proper error responses

The webapp should now work perfectly!