Php 我一直在获取此错误文件\u get\u contents(无法打开流:HTTP请求失败!HTTP/1.1 400错误请求
我正在学习instagram教程。我已经检查了大约一个小时的代码,似乎没有发现问题。我一直收到以下错误Php 我一直在获取此错误文件\u get\u contents(无法打开流:HTTP请求失败!HTTP/1.1 400错误请求,php,instagram-api,laravel-5.1,Php,Instagram Api,Laravel 5.1,我正在学习instagram教程。我已经检查了大约一个小时的代码,似乎没有发现问题。我一直收到以下错误 file_get_contents(https://api.instagram.com/v1/media/search?lat33.810485&lng=-117.918989&client_id=b2b2dccbfc432681097): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD
file_get_contents(https://api.instagram.com/v1/media/search?lat33.810485&lng=-117.918989&client_id=b2b2dccbfc432681097): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST
视图:/home/vagrant/Code/api/resources/views/welcome.blade.php
它指向第十三行
哪个是标记的
我拥有视图中的所有内容。我正在使用laravel 5.1。所有内容似乎都是最新的。这是我的代码
<?php
if(!empty($_GET['location'])){
$maps_url = 'https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($_GET['location']);
$maps_json = file_get_contents($maps_url);
$maps_array = json_decode($maps_json, true);
$lat = $maps_array['results'][0]['geometry']['location']['lat'];
$lng = $maps_array['results'][0]['geometry']['location']['lng'];
$instagram_url = 'https://api.instagram.com/v1/media/search?lat'.$lat.'&lng='.$lng.'&client_id=b2b2dccbfc432681097';
$instagram_json = file_get_contents($instagram_url);
$instagram_array = json_decode($instagram_json, true);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>api geocode</title>
<link href="//fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
<style>
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato';
}
.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content {
text-align: center;
display: inline-block;
}
.title {
font-size: 96px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Laravel 5</div>
<form action="">
<input type="text" name="location">
<button type="submit">submit</button>
<br>
<?php
if(!empty($instagram_array)){
foreach($instagram_array['data'] as $image ){
echo '<img src="'.$image['images']['low_resolution']['url'].' "
alt=""/>';
}
}
?>
</form>
</div>
</div>
</body>
</html>
感谢您的帮助。这是您的问题:
$maps_url = 'https://maps.googleapis
.com/maps/api/geocode/json?address='.
urlencode($_GET['location']);
您有一个地址,其中有一个新行字符和一些空格。这是将地址传递给文件\u get\u contents时的样子:
您可以通过将其拆分为两个字符串或将其放在一行来解决此问题:
$maps_url = 'https://maps.googleapis' .
'.com/maps/api/geocode/json?address='.
urlencode($_GET['location']);
instagram URL也有同样的问题。在复制和粘贴代码时,uri中有一个输入错误。它缺少lat密钥-值对的等号。instagram已更改其api。您不能再在端点中使用客户端id。相反,您必须请求身份验证,接收访问令牌并传递t他的访问令牌而不是客户端id 以下是instagram开发者API的链接
您是否尝试过将url编写为不带换行符的字符串?
$maps_url = 'https://maps.googleapis' .
'.com/maps/api/geocode/json?address='.
urlencode($_GET['location']);