简体   繁体   中英

I keep getting this error file_get_contents(failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST

I am following a an instagram tutorial. I have gone over my code for about an hour and can't seem to find the problem. I keep getting the following error

 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

(View: /home/vagrant/Code/api/resources/views/welcome.blade.php)

its pointing to line thirteen

which is marked

I have everything in the view. I am using laravel 5.1. Everything seems to be up to date. Heres my code

<?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>

any help wil be appreciated.

This is your problem:

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

You have an address with a new line character (and some spaces) in it. This is what the address looks like when you pass it to file_get_contents() :

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

You can fix this by either splitting it into two strings or putting it on one line:

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

The instagram URL suffers from the same problem.

WhenI was copy and pasting code there was a typo in the uri. It was missing an equal sign for the lat key value pair

Instagram has changed its api. You cannot use the client_id in the endpoint anymore. Instead you have to request for authentication, receive the access token and pass this access token instead of client_id.

Here's the link for the instagram developer API https://www.instagram.com/developer/endpoints/media/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM