I’m working with a travel booking API and trying to create a new search session using PHP. The API should return a location header in the response that I need for subsequent requests, but I keep getting a 405 Method Not Allowed error instead.
Here’s my current code:
require_once 'vendor/autoload.php';
require_once 'vendor/mashape/unirest-php/src/Unirest.php';
$apiResponse = Unirest\Request::post("https://travel-api-service.p.rapidapi.com/flights/search/v2.0",
array(
"X-RapidAPI-Host" => "travel-api-service.p.rapidapi.com",
"X-RapidAPI-Key" => "your-api-key-here",
"Content-Type" => "application/x-www-form-urlencoded"
),
array(
"departureDate" => "2024-06-15",
"travelClass" => "economy",
"childCount" => 0,
"infantCount" => 0,
"countryCode" => "US",
"currencyCode" => "USD",
"localeCode" => "en-US",
"fromAirport" => "LAX-sky",
"toAirport" => "JFK-sky",
"returnDate" => "2024-06-20",
"passengerCount" => 2
)
);
var_dump($apiResponse);
I expect to get headers like this:
cache-control: private
content-type: application/json
date: Thu, 15 Jun 2024 10:30:45 GMT
location: http://api.travel-service.net/flights/search/us1/v2.0/abc123-def456-ghi789
server: RapidAPI-1.0.20
But instead I’m getting a 405 error. The response shows Method Not Allowed but I’m sure I’m using POST correctly. Has anyone encountered this issue before? What could be causing this problem?