uzz回流?

时间:2018-10-27 15:13:58

标签: php laravel-5 guzzle

我有这个:

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

响应应为:

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

但是我却得到:

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 个答案:

答案 0 :(得分:0)

我认为您需要添加getContents()
这个答案对您有用: Guzzlehttp - How get the body of a response from Guzzle 6?
请尝试此代码。

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