首页 > 解决方案 > Google Places Nearby API Success 200,但在 Laravel 中返​​回“INVALID_REQUEST”

问题描述

我正在尝试为 Google Places API 创建一个迭代爬虫。任何使用过他们的 API 的人都知道,响应每页仅包含 20 个结果,我正在尝试获取完整的结果列表,因此我创建了一个循环,如果 next_page_token 字段存在,它将执行一个函数。最奇怪的事情发生了,因为它成功检索并回显了前 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) ,第二个函数被调用,但在正文中返回“INVALID_REQUEST”。这非常奇怪,因为从使用但失败的第二个函数中获取确切的实际查询字符串并将其放入我的 URL 栏中会返回正确的响应,从而排除 API 调用中的任何拼写错误。

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

标签: phpjsonlaravelgoogle-apigoogle-places-api

解决方案


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

$results->push($result;      

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


推荐阅读