PHP POST request to flight booking API returns 405 Method Not Allowed error

I’m working with a travel booking API using PHP and trying to create a new search session. The API should return a location header in the response, but instead I keep getting a 405 Method Not Allowed error. All other response data comes through fine, just missing that location header I need.

require_once 'vendor/autoload.php';
require_once 'vendor/mashape/unirest-php/src/Unirest.php';

$apiResponse = Unirest\Request::post("https://travel-api-v2.p.rapidapi.com/flights/search/create",
  array(
    "X-RapidAPI-Host" => "travel-api-v2.p.rapidapi.com",
    "X-RapidAPI-Key" => "abc123def456ghi789jkl012mno345pqr678stu901vwx234yz",
    "Content-Type" => "application/x-www-form-urlencoded"
  ),
  array(
    "departDate" => "2024-05-15",
    "classType" => "economy",
    "childCount" => 0,
    "infantCount" => 0,
    "countryCode" => "US",
    "currencyCode" => "USD",
    "language" => "en-US",
    "fromAirport" => "JFK-airport",
    "toAirport" => "LAX-airport",
    "returnDate" => "2024-05-20",
    "passengerCount" => 2
  )
);

var_dump($apiResponse);

The response I’m hoping to get looks like this:

"cache-control": "no-cache"
"content-type": "application/json"
"date": "Thu, 15 May 2024 10:30:45 GMT"
"location": "http://api.travel-service.com/search/sessions/abc-def-123-456-789"
"server": "TravelAPI-2.1.0"
"x-api-region": "US-East-1"
"x-api-version": "2.1.0"
"content-length": "0"
"connection": "Close"

But what I actually get is this error:

Unirest\Response Object ( [code] => 405 [raw_body] => {} [body] => stdClass Object ( ) [headers] => Array ( [0] => HTTP/1.1 405 Method Not Allowed [Cache-Control] => no-cache [Content-Type] => application/json [Date] => Thu, 15 May 2024 10:30:45 GMT [Server] => TravelAPI-2.1.0 [X-API-Region] => US-East-1 [X-API-Version] => 2.1.0 [Content-Length] => 2 [Connection] => keep-alive ) )

Anyone know what might be causing this issue?

Check if that endpoint actually exists on RapidAPI - I’ve seen APIs where the docs show one URL but the working endpoint is different. Hit it with a simple GET first to see if it responds. Also, some travel APIs need an initial auth call before search requests work.

Your authentication with RapidAPI looks fine, but I think there’s something else going on. I’ve dealt with similar travel APIs through RapidAPI before - sometimes it’s not just the content type that’s the problem. Try adding a User-Agent header. Some APIs won’t work without one. Also, double-check that your RapidAPI plan actually covers that specific endpoint - they restrict certain methods based on subscription tiers. Another thing - check how you’re formatting those airport codes. I’ve seen APIs reject ‘JFK-airport’ format and only accept ‘JFK’. Since you’re getting a proper API response structure back, authentication isn’t the issue. It’s probably just how you’re formatting the request.

That 405 error means the endpoint doesn’t accept POST requests or something’s wrong with your request structure. I’ve hit this exact issue with travel APIs - they wanted JSON payload, not form data. Switch your Content-Type header to “application/json” and wrap your parameters with json_encode() instead of sending form data. Double-check the API docs too. Some endpoints need specific auth flows or extra headers beyond just the RapidAPI key. Also verify that endpoint URL is right - sometimes it’s /sessions/create instead of /search/create.

Manual API debugging is a pain. Been there with travel APIs - they’re picky as hell.

405 error + no location header = your request format’s wrong. Skip the guessing game with headers and payloads - automate it.

I use Latenode for API work like this. Build requests visually, test different headers, handles JSON encoding automatically. Once it works, turn it into a full booking workflow - search to results.

Visual interface makes spotting issues like wrong content types easy. Clone working requests to test changes without rewriting code.

Saves hours vs manual PHP debugging. Check https://latenode.com