0

I am getting the following error when I am trying to pass url values into "file_get_content" :

file_get_contents(https://api.sandbox.amadeus.com/v1.2/flights/low-fare-search?origin=$origin&destination=$destination&departure_date=$departure_date&return_date=$return_date&number_of_results=$number_of_results&apikey=$apikey): failed to open stream: HTTP request failed! HTTP/1.1 401

I read different possible solutions, but none of them seemed to work in my case

$origin = $_GET['origin'];
$destination = $_GET['destination'];
$departure_date = $_GET['departure_date'];
$return_date = $_GET['return_date'];
$number_of_results = $_GET['number_of_results'];
$apikey = "sdflsdksdksdsdskd";

ini_set("allow_url_fopen", 1);

$json_url = file_get_contents('https://api.sandbox.amadeus.com/v1.2/flights/low-fare-search?origin=$origin&destination=$destination&departure_date=$departure_date&return_date=$return_date&number_of_results=$number_of_results&apikey=$apikey');

The result from the $json_url is a simple valid JSON Any help will be highly appreciated.

EDIT: The problem is not with the API Key, when I hard-code it, the error code is different (400 Bad Request), but still file_get_contents - failed to open stream: HTTP request failed

lStoilov
  • 1,256
  • 3
  • 14
  • 30
  • 1
    401 means unauthorized. So you are probably not authenticated correctly. – Ivar Aug 06 '18 at 07:14
  • Of course, the $apikey is not taken as well as the other values... but even if I hardcode the key the result is the same, apart of the API Key error which is 400 Bad Request – lStoilov Aug 06 '18 at 07:19
  • if using the key the result is 400 bad request, then the result isn't the same, right? – eis Aug 06 '18 at 07:21
  • 1
    Someone needs to read http://php.net/manual/en/language.types.string.php first of all. – CBroe Aug 06 '18 at 07:24
  • When I hard-code all the values it works with no problem, but, but the thing here is that I am getting the values from the url and then creating the new fopen url – lStoilov Aug 06 '18 at 07:25
  • @CBroe and what should I read there? – lStoilov Aug 06 '18 at 07:30
  • 1
    @lStoilov That you should use double quotes instead of single quotes for the string in your `file_get_contents`. – Ivar Aug 06 '18 at 07:32
  • Don't use `file_get_contents`, because it sucks. It fails to return the output if the response is other than `2XX`. Better use `Guzzle` instead. – Mike Doe Aug 06 '18 at 07:35

2 Answers2

4

The problem is that your url is read literally due to '.
If you use " then the variables in the string will be used as variables but now your string is '...apikey=$api' instead of '...apikey=sdflsdksdksdsdskd'

So change the line

$json_url = file_get_contents('https://api.sandbox.amadeus.com/v1.2/flights/low-fare-search?origin=$origin&destination=$destination&departure_date=$departure_date&return_date=$return_date&number_of_results=$number_of_results&apikey=$apikey');

To

$json_url = file_get_contents("https://api.sandbox.amadeus.com/v1.2/flights/low-fare-search?origin=$origin&destination=$destination&departure_date=$departure_date&return_date=$return_date&number_of_results=$number_of_results&apikey=$apikey");
Andreas
  • 23,610
  • 6
  • 30
  • 62
3

Like mentioned in the comment, HTTP 401 means unauthorized, so you're not supplying correct authentication for the request. In addition, if you open the url in the browser or read the message body of the response, you see that it says

"A valid API key is required to access this resource. Please ensure your request includes a query parameter called apikey, whose value is a valid API key from your account at https://sandbox.amadeus.com"

which should explain the issue quite clearly: you're not sending the api key.

The root cause of your problem would look to me like this:

file_get_contents('https://api.sandbox.amadeus.com/v1.2/flights/low-fare-search?origin=$origin&destination=$destination&departure_date=$departure_date&return_date=$return_date&number_of_results=$number_of_results&apikey=$apikey');

You're using single quotes (') here, but if you want to use variables such as $departure_date within your string like that, you need to use double quotes (") for your string.

eis
  • 51,991
  • 13
  • 150
  • 199
  • Already answered above... the problem is not the authorization. With the hard-coded api (correct api) the result is failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request – lStoilov Aug 06 '18 at 07:21