【问题标题】:403 calling api.weather.gov using Buzz client w/ https403 使用带有/ https 的 Buzz 客户端调用 api.weather.gov
【发布时间】:2017-09-08 20:47:02
【问题描述】:

尝试在我的 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 )

【问题讨论】:

  • 向我们展示您的代码
  • 对不起@ATechGuy,刚刚添加了我正在使用的内容

标签: php curl noaa buzz


【解决方案1】:

您需要设置AcceptVersionUser-Agent 标头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2017-04-01
    • 2011-01-09
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多