Skip to content

Instantly share code, notes, and snippets.

@pradeeprjth
Created February 10, 2022 08:55
Show Gist options
  • Save pradeeprjth/3a7c1be5070a5bc3169f85ff2ac2819b to your computer and use it in GitHub Desktop.
Save pradeeprjth/3a7c1be5070a5bc3169f85ff2ac2819b to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
class WebPageSizeCheckerController extends Controller
{
public function post(Request $request)
{
$url = $request->url;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$subject = curl_exec($curl);
//get the download size of page
print("Download size 1: " . curl_getinfo($curl, CURLINFO_SIZE_DOWNLOAD) .'<br>');
preg_match_all('/(?:src=)"([^"]*)"/m', $subject, $matchessrc);
preg_match_all('/link.*\s*(?:href=)"([^"]*)"/m', $subject, $matcheslink);
$matches = array_merge($matchessrc[1], $matcheslink[1]);
$domain = parse_url($url, PHP_URL_SCHEME). '://'.parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$checked = array();
foreach($matches as $m)
{
if($m[0] == '/')
$m = $domain.$m;
elseif(substr($m, 0, 5) != 'http:' and substr($m, 0, 6) != 'https:')
$m = $domain.'/'.$path.'/'.$m;
if(in_array($m, $checked))
continue;
$checked[] = $m;
$curl = curl_init($m);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$subject = curl_exec($curl);
//get the download size of element
print("Download size 2: " . curl_getinfo($curl, CURLINFO_SIZE_DOWNLOAD) .'<br>');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment