2017-06-05 13 views
0

Google API PageSpeed Insightsのスクリーンショットを取得する際に問題が発生していますが、APIを使用したときにウェブサイトを提供しています。私は他の例を見てきましたが、私にはうまくいかないものはありません。google page insights 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 }}"> 
関連する問題