Google 附近的地方 API 成功 200,但 returns "INVALID_REQUEST" 在 Laravel

Google Places Nearby API Success 200, but returns "INVALID_REQUEST" in Laravel

我正在尝试为 Google 个地点 API 制作一个迭代爬虫。任何使用过 API 的人都知道,响应每页只包含 20 个结果,我试图获得完整的结果列表,所以我创建了一个循环,如果 [=24] =] 字段存在。最奇怪的事情发生了,因为它成功检索并回显了前 20 个结果,但未能执行第二次搜索,但没有在 Laravel 中抛出任何错误。代码如下(在 Laravel)

public function getAllPlaces(){
        if (Auth::user()->id != '5') {
            return redirect('/userlanding')->with('error', 'Denied access');
        } else {
            $client = new Client();

            $types = 'park';

            $radius = '50000';

            $location = [

                'latitude' => '36.187740',

                'longitude' => '-92.731422'

            ];

            $keyword = '';

            $requestQuery = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$location['latitude']},{$location['longitude']}&radius={$radius}&type={$types}{$keyword}&key=MYSUPERSECRETKEY";

            $response = $client->request('GET', $requestQuery);
            $statusCode = $response->getStatusCode();
            $body = $response->getBody()->getContents();

            $body = json_decode($body);

            $numofrecordspresent = 0;

            $data = $body->results;

            $results = collect();
            foreach($data as $result) {
                 $results->push($result;      
                 $numofrecordspresent = $numofrecordspresent + 1;
                }
            }

            if(isset($body->next_page_token)) {
                $loopData = [

                    'nexttoken' => $body->next_page_token,

                    'locationData' => $location,

                    'numofrecordspresent' => $numofrecordspresent,

                    'radius' => $radius,

                    'types' => $types,

                    'keyword' => $keyword,

                    'client' => $client,

                ];

                $this->loopedPlaceSearch($loopData);
            } else {
                return view('apireturnpage')->with('numofrecordspresent', $numofrecordspresent);
            }
        }
    }

    public function loopedPlaceSearch($loopData) {
        $client = new Client();

        $location = $loopData['locationData'];
        $numofrecordspresent = $loopData['numofrecordspresent'];
        $pagetoken = $loopData['nexttoken'];
        $radius = $loopData['radius'];
        $types = $loopData['types'];
        $keyword = $loopData['keyword'];

        $requestQuery = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={$location['latitude']},{$location['longitude']}&radius={$radius}&type={$types}{$keyword}&pagetoken={$pagetoken}&key=MYSUPERSECRETKEY";

        $response = $client->request('GET', $requestQuery);
        $statusCode = $response->getStatusCode();
        $body = $response->getBody()->getContents();

        $body = json_decode($body);

        $data = $body->results;

        $results = collect();
        foreach($data as $result) {
             $results->push($result;      
             $numofrecordspresent = $numofrecordspresent + 1;
            }
        }

        if (isset($body->next_page_token)) {
            $loopData = [

                'nexttoken' => $body->next_page_token,

                'locationData' => $location,

                'numofrecordspresent' => $numofrecordspresent,

            ];


            $this->loopedPlaceSearch($loopData);
        } else {
            return view('apireturnpage')->with('numofrecordspresent', $numofrecordspresent);
        }
    }

目前,为了详细说明我之前所说的,从 web.php 调用的第一个函数工作正常,将所有 20 个结果推送到结果集合中,但是当有更多结果时(即 next_page_token 存在),第二个函数被调用但 returns "INVALID_REQUEST" 在主体中。这非常奇怪,因为从使用但失败的第二个函数中获取确切的实际查询字符串并将其放入我的 URL 栏 returns 正确的响应,排除 [=26= 中的任何拼写错误] 打电话。

任何帮助将不胜感激,这似乎是一个错误,我无法弄清楚为什么它不起作用。

您的两个结果推送似乎都缺少右括号:

$results->push($result;      

我不知道这是否会解决它,但它困扰着我!

我做这个 post 已经有一段时间了,但是对于那些现在发现这个问题的人来说,我想我会解释为什么会发生这种情况以及我是如何解决它的。事实证明,在脚本中没有延迟的情况下,服务器尝试发出另一个请求,而另一个响应仍在返回,这导致了问题。通过在发出每个请求之前添加一个 sleep() 或 usleep() 来非常简单地解决这个问题,以防止同时调用多个。

编辑(5/10/20):这个解决方案仍然有效,但推理与我解释的不一样。使用 usleep() 或 sleep() 函数解决此问题的真正原因是 Google 在其 API 端点上实施了瓶颈以防止误用。它只允许每 1-3 秒发出一个请求,具体取决于 API,如果再这样会导致此错误发生。我只知道这一点,因为我直接联系了 Google,这是我从 Google.

的技术支持员工那里收到的回复
  1. 在 google 位置 api.

  2. 另一点是你对 loopedPlaceSearch() 函数的递归调用。您将响应放在一个变量中,该变量的范围由他自己的函数限制。最好你把你的回复交给班级成员。