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

I’m working with a travel booking API through RapidAPI and having trouble with creating a flight search session. When I send a POST request to initialize the search, I keep getting a 405 Method Not Allowed error instead of the expected location header in the response.

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

$apiResponse = Unirest\Request::post("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0",
  array(
    "X-RapidAPI-Host" => "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
    "X-RapidAPI-Key" => "abc123def456ghi789jkl012mno345pqr678stu901vwx234yz",
    "Content-Type" => "application/x-www-form-urlencoded"
  ),
  array(
    "returnDate" => "2024-06-15",
    "seatClass" => "economy",
    "childCount" => 0,
    "infantCount" => 0,
    "countryCode" => "US",
    "currencyCode" => "USD",
    "localeCode" => "en-US",
    "departureAirport" => "LAX-sky",
    "arrivalAirport" => "JFK-sky",
    "departureDate" => "2024-06-10",
    "passengerCount" => 2
  )
);

var_dump($apiResponse);

I should be getting headers like this:

"cache-control": "private"
"content-type": "application/json" 
"date": "Thu, 15 Feb 2024 10:30:22 GMT"
"location": "http://partners.api.skyscanner.net/apiservices/pricing/us2/v1.0/8f3e21a5-902b-4c7a-b8f4-d9e6c5a4b3f2"
"server": "RapidAPI-1.0.20"

But instead I’m getting a 405 error with empty response body. The API documentation says this endpoint should accept POST requests, so I’m not sure what’s wrong with my implementation. Has anyone encountered this issue before?

Your endpoint URL looks incomplete. With this API, you need the full path to the session creation endpoint. Skyscanner uses different endpoints for creating sessions vs. getting results from existing ones. Also check your API key has the right permissions for the pricing service - some subscription levels block certain endpoints. When I ran into this, it was usually because I was POSTing to a read-only endpoint instead of hitting the session initialization one.

Had the same issue with Skyscanner API through RapidAPI. You’re missing /create at the end of your URL - that’s why it’s not working. Your URL should be https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0/create. The base URL you’re using just retrieves existing sessions, doesn’t create new ones. Also check if your RapidAPI plan actually covers the pricing endpoint - some basic plans don’t include it. Once you hit the correct create endpoint, you’ll get a session URL in the location header that you can poll for results.

hmmm, I had that issue too. Try addin a trailing slash to the end of ur URL or see if you need /create there. Also, lemme remind ya to check your RapidAPI subcription if it’s still active and has the right permissions!

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