Guzzle Returning Stream? [duplicate]

徘徊边缘 提交于 2020-01-06 11:43:05

问题


I have this:

private function logSearch($term)
    {
        $geo_data = $this->geoIP($ip);
        dd($geo_data);
    }

public function geoIP($ip)
    {
        $url = "http://api.ipstack.com/$ip?access_key=" . env('GEOIP_KEY');    
        $response = $this->client->request('GET', $url);
        return $response->getBody();
    }

The response should be:

{"ip":"78.63.56.237","type":"ipv4","continent_code":"EU"...etc}

But instead I am getting:

Stream {#482
  -stream: stream resource @290
    wrapper_type: "PHP"
    stream_type: "TEMP"
    mode: "w+b"
    unread_bytes: 0
    seekable: true
    uri: "php://temp"
    options: []
  }
  -size: null
  -seekable: true
  -readable: true
  -writable: true
  -uri: "php://temp"
  -customMetadata: []
}

回答1:


I think you need to add getContents().
This answer would be useful for you: Guzzlehttp - How get the body of a response from Guzzle 6?
Please try this code.

private function logSearch($term)
{
    $geo_data = $this->geoIP($ip);
    dd($geo_data);
}

public function geoIP($ip)
{
    $url = "http://api.ipstack.com/$ip?access_key=" . env('GEOIP_KEY');    
    $response = $this->client->request('GET', $url);
    return $response->getBody()->getContents();
}


来源:https://stackoverflow.com/questions/53023314/guzzle-returning-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!