Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

How to improve an API call to Google

Hey!

I'm using Google APIs to retrieve a screenshot of a specific website. After getting the API response I'm creating a jpg using Image Intervention. The GET request I'm sending:

https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={WEBSITE_LINK}&screenshot=true

and my code looks like this:

if(
(
$api_response = @file_get_contents("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" . $websiteURL . "&screenshot=true")
) !== false
) {
     $result = json_decode($api_response, true);
     $screenshot1 = $result['lighthouseResult']['audits']['final-screenshot']['details']['data'];
     $screenshot = str_replace(array('_','-'),array('/','+'),$screenshot1);
     $screenshot_name = 'logos/' . $request->domain . '.jpg';
     Image::make(file_get_contents($screenshot1))->resize(120, 120)->save(public_path() . '/' . $screenshot_name);
}

But it's way too slow. Can you suggest me something to speed up this process? Do you have any idea why it takes up to 30 seconds to get a response from this GET request?

0 likes
4 replies
Sinnbeck's avatar

Most likely because it makes alot of different tests on the page. Is a screenshot of the page all you need? If so try browsershot

lat4732's avatar
Level 12

@Sinnbeck Well, I'm of the opinion that Google is Google, that's why I'm using it. In general, I check through this API whether the site exists in addition to everything else. It would be great if I can also retrieve a short description of the website as well but I don't know how exactly to do it.

Is there a way of disabling certain test within this API so I can improve its speed?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Laralex well that url is specifically pagespeedonline. So it makes sense that it tests stuff. Consider breaking it up into smaller tests

  1. Check site exists
  2. Take picture
  3. Get meta data
lat4732's avatar
Level 12

@Sinnbeck,

According to my customer I need to find a library that don't use nodejs. No idea why. Can you suggest me some library for taking screenshot of a URL which doesn't require nodejs to work?

Thanks in advance!

Please or to participate in this conversation.