【问题标题】:I keep getting this error file_get_contents(failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST我不断收到此错误 file_get_contents(未能打开流:HTTP 请求失败!HTTP/1.1 400 BAD REQUEST
【发布时间】:2015-11-08 06:00:20
【问题描述】:

我正在关注 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 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>

我们将不胜感激。

【问题讨论】:

  • 你试过把url写成不换行的字符串吗?

标签: php instagram-api laravel-5.1


【解决方案1】:

这是你的问题:

$maps_url = 'https://maps.googleapis
  .com/maps/api/geocode/json?address='.
  urlencode($_GET['location']);

您的地址中有一个换行符(和一些空格)。这是地址传递给file_get_contents()时的样子:

'https://maps.googleapis\n      .com/maps/api/geocode/json?address='

您可以通过将其拆分为两个字符串或将其放在一行来解决此问题:

$maps_url = 'https://maps.googleapis' .
  '.com/maps/api/geocode/json?address='.
  urlencode($_GET['location']);

instagram URL 也有同样的问题。

【讨论】:

    【解决方案2】:

    当我复制和粘贴代码时,uri 中有一个错字。 lat 键值对缺少等号

    【讨论】:

      【解决方案3】:

      Instagram 已更改其 API。您不能再在端点中使用 client_id。相反,您必须请求身份验证,接收访问令牌并传递此访问令牌而不是 client_id。

      这是 Instagram 开发者 API 的链接 https://www.instagram.com/developer/endpoints/media/

      【讨论】:

        猜你喜欢
        • 2017-08-07
        • 1970-01-01
        • 2011-05-31
        • 1970-01-01
        • 2012-03-23
        • 2016-06-02
        • 2020-12-18
        • 1970-01-01
        相关资源
        最近更新 更多