dongyao1915 2017-04-12 21:51
浏览 97
已采纳

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

Trying to use curl/file_get_contents using Buzz in my Symfony application to retrieve data like from what you could see here:

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

No matter what, I seem to get a 403 Forbidden error and I'm wondering if anyone has any advice?

    $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();

In Buzz' FileGetContents class

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

$url evaluates to : https://api.weather.gov/points/44.353700,-73.863600/forecast/hourly

Same with CURL

Headers returned

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条回答 默认 最新

  • dongshao8471 2017-07-12 20:58
    关注

    You need to set the Accept, Version, and User-Agent headers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退