file_get_contents-无法打开流:HTTP请求失败

时间:2018-08-06 07:12:31

标签: php json file-get-contents

当我尝试将url值传递到“ 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):   无法打开流:HTTP请求失败! HTTP / 1.1 401

我阅读了各种可能的解决方案,但在我看来,这些解决方案都不起作用

$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');

$ json_url的结果是一个简单的有效JSON 任何帮助将不胜感激。

编辑: 问题不在于API密钥,当我对其进行硬编码时,错误代码有所不同(400错误请求),但仍然是file_get_contents-无法打开流:HTTP请求失败

2 个答案:

答案 0 :(得分:2)

就像评论中提到的那样,HTTP 401表示未经授权,因此您没有为请求提供正确的身份验证。另外,如果您在浏览器中打开网址或阅读响应的消息正文,则会看到它显示为

  

“访问此资源需要有效的API密钥。请确保您的请求中包含一个名为   apikey,其值为您帐户中的有效API密钥,网址为   https://sandbox.amadeus.com

这应该很清楚地解释这个问题:您没有发送api密钥。

您的问题的根本原因是这样的:

  

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');

您在此处使用单引号('),但是如果您想在字符串中使用$ departure_date之类的变量,则需要在双引号(")中使用字符串。

答案 1 :(得分:1)

问题是,由于"的原因,您的url确实被读取了。 如果您使用'...apikey=$api',则字符串中的变量将用作变量,但是现在您的字符串是'...apikey=sdflsdksdksdsdskd'而不是$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');

所以换行

$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");

收件人

log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern=%d [%t] %-5p %c - 
%m%n
#The noisier spark logs go to file only
log4j.logger.spark.storage=INFO, myappender
log4j.additivity.spark.storage=false
log4j.logger.spark.scheduler=INFO, myappender
log4j.additivity.spark.scheduler=false
log4j.logger.spark.CacheTracker=INFO, myappender
log4j.additivity.spark.CacheTracker=false
log4j.logger.spark.CacheTrackerActor=INFO, myappender
log4j.additivity.spark.CacheTrackerActor=false
log4j.logger.spark.MapOutputTrackerActor=INFO, myappender
log4j.additivity.spark.MapOutputTrackerActor=false
log4j.logger.spark.MapOutputTracker=INFO, myappender
log4j.additivty.spark.MapOutputTracker=false
log4j.org.apache.spark.SparkContext=INFO, myappender
log4j.additivity.spark.SparkContext=false

log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappender.File=/tmp/sparkLogs.log
log4j.appender.myappender.DatePattern='.'yyyy-MM-dd
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.layout.ConversionPattern=[%p] %d %c %M - %m%n

log4j.rootLogger=INFO,myConsoleAppender