使用php获取另一个域上的js文件的文件大小

发布于 2024-12-25 22:19:44 字数 220 浏览 0 评论 0原文

如何获取另一个网站上的js文件的文件大小。我正在尝试创建一个监视器来检查 js 文件是否存在并且它是否超过 0 字节。

例如,在 bar.com 上我将有以下代码:

$filename = 'http://www.foo.com/foo.js';
echo $filename . ': ' . filesize($filename) . ' bytes';

How do I get the filesize of js file on another website. I am trying to create a monitor to check that a js file exists and that it is more the 0 bytes.

For example on bar.com I would have the following code:

$filename = 'http://www.foo.com/foo.js';
echo $filename . ': ' . filesize($filename) . ' bytes';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飘然心甜 2025-01-01 22:19:44

您可以使用 HTTP HEAD 请求。

<?php
 $url = "http://www.neti.ee/img/neti-logo.gif";
 $head = get_headers($url, 1);
 echo $head['Content-Length'];
?>

注意:这不是真正的 HEAD 请求,而是 PHP 解析其 Content-Length 的 GET 请求。不幸的是,PHP 函数名称非常具有误导性。这对于小型 js 文件可能就足够了,但对于较大的文件大小,请使用带有 Curl 的真正 HTTP Head 请求,因为这样服务器就不必上传整个文件而只需发送标头。

对于这种情况,请使用 Jakub 提供的代码。

You can use a HTTP HEAD request.

<?php
 $url = "http://www.neti.ee/img/neti-logo.gif";
 $head = get_headers($url, 1);
 echo $head['Content-Length'];
?>

Notice: this is not a real HEAD request, but a GET request that PHP parses for its Content-Length. Unfortunately the PHP function name is quite misleading. This might be sufficient for small js files, but use a real HTTP Head request with Curl for bigger file sizes because then the server won't have to upload the whole file and only send the headers.

For that case, use the code provided by Jakub.

不奢求什么 2025-01-01 22:19:44

只需使用 CURL,这里列出了一个非常好的示例:
参考: http://www.php。净/手册/en/function.filesize.php#92462

<?php
$remoteFile = 'http://us.php.net/get/php-5.2.10.tar.bz2/from/this/mirror';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}

$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
  $status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>

结果:

HTTP 状态:302
内容长度:8808759

Just use CURL, here is a perfectly good example listed:
Ref: http://www.php.net/manual/en/function.filesize.php#92462

<?php
$remoteFile = 'http://us.php.net/get/php-5.2.10.tar.bz2/from/this/mirror';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}

$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
  $status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>

Result:

HTTP Status: 302
Content-Length: 8808759

辞慾 2025-01-01 22:19:44

这只是一个两步过程:

  1. 抓取 js 文件并将其存储到一个变量中
  2. 检查 js 文件的长度是否大于 0

就是这样!

以下是在 PHP 中执行此操作的方法

<?php

$data = file_get_contents('http://www.foo.com/foo.js');

if(strlen($data)>0):
    echo "yay"
else:
    echo "nay"
?>

注意:您可以按照 Uku 的建议使用 HTTP Head,但是如果您正在寻找页面内容,如果 js 文件有内容,那么您将不得不再次抓取:(

This is just a two step process:

  1. Crawl the the js file and store it to a variable
  2. Check if the length of the js file is greater than 0

thats it!!

Here is how you can do it in PHP

<?php

$data = file_get_contents('http://www.foo.com/foo.js');

if(strlen($data)>0):
    echo "yay"
else:
    echo "nay"
?>

Note: You can use HTTP Head as suggested by Uku but then if you are seeking for the page content if js file has content then you would have to crawl again :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文