403使用Buzz客户端w / https调用api.weather.gov

时间:2017-04-12 21:51:44

标签: php curl noaa buzz

尝试在我的Symfony应用程序中使用Buzz来使用curl / file_get_contents来检索数据,例如你可以在这里看到的数据:

https://api.weather.gov/points/44.3537,-73.8636/forecast/hourly

无论如何,我似乎得到403 Forbidden错误,我想知道是否有人有任何建议?

    $location = sprintf(
        '/points/%f,%f/forecast/hourly', $this->latitude, $this->longitude
    );

    $request  = new Request('GET', $location, 'https://api.weather.gov');
    $response = new Response();

    try {
        $this->httpClient->send($request, $response);
    } catch (\Exception $e) {
        throw new ServiceResponseException('Failed to send Request', 0, $e);
    }

    if (!$response->isSuccessful()) {
        throw new ServiceResponseException('Unsuccessful Response', $response->getStatusCode());
    }

    return $response->getContent();

在Buzz'FileGetContents类

$url = $request->getHost().$request->getResource();

$ url评估为: https://api.weather.gov/points/44.353700,-73.863600/forecast/hourly

与CURL相同

标题返回

Array ( [0] => HTTP/1.1 403 Forbidden [1] => Server: AkamaiGHost [2] => Mime-Version: 1.0 [3] => Content-Type: text/html [4] => Content-Length: 334 [5] => Expires: Thu, 13 Apr 2017 14:15:56 GMT [6] => Date: Thu, 13 Apr 2017 14:15:56 GMT [7] => Connection: close )

1 个答案:

答案 0 :(得分:1)

您需要设置AcceptVersionUser-Agent标题。