2017-06-05 72 views
0

我无法获得Google API PageSpeed Insights截图,他们提供的网站在击中API时。我看过其他例子,但似乎没有一个适合我。谷歌页面洞察api截图 - Laravel

这就是我在打API时得到的结果。 Google API Endpoint

这是我的控制器:

public function fetch() { 

     $key = env('GOOGLE_ANALYTICS_KEY'); 
     $website = request('website_url'); 

     $client = new \GuzzleHttp\Client(); 

     $res = $client->request('GET', 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=http://' .$website. '&key=' . $key . '&screenshot=true'); 

     if ($res->getStatusCode() == 200) { 
      $result = json_decode($res->getBody()); 

      $data = str_replace('_','/',$result->screenshot->data); 
      $data = str_replace('-','+',$data); 
      $decoded = base64_decode($data); 

      return view('api.index', compact('result', 'decoded')); 

     } else { 
      return redirect()->back(); 
     } 
    } 

我尝试调用它的前端是这样的:

<img src="data:image/jpeg;base64,'{{ $decoded }}"> 
// And like this 
<img src="{{ $decoded }}"> 

但仅仅得到一个空的src = “”

+0

你的第一个''标记看起来像它最后缺少一个''''。 – ceejayoz

+0

没看见。我修好了它。尽管如此,它仍然是空虚的。 – David

+0

查看源代码时是否将数据输出到页面?你为什么要替换数据中的'_'和'-'? – ceejayoz

回答

0

我想通了。

这是我做我的控制器(没有太大的变化):

$result = json_decode($res->getBody()); 

      $data = str_replace('_','/',$result->screenshot->data); 
      $data = str_replace('-','+',$data); 

这是我如何在前端把它称为:

<img src="data:image/jpeg;base64,{{ $data }}">