gpt4 book ai didi

php - 检查文件(robots.txt,favicon.ico)到网站 php

转载 作者:可可西里 更新时间:2023-10-31 22:49:55 26 4
gpt4 key购买 nike

我想检查远程网站是否包含一些文件。例如。 robots.txtfavicon.ico。当然,文件应该是可访问的(读取模式)。

所以如果网站是:http://www.example.com/ 我想检查 http://www.example.com/robots.txt.

我尝试获取像 http://www.example.com/robots.txt 这样的 URL。有时您可以查看文件是否存在,因为您在页眉中收到页面未找到错误。

但是一些网站会处理这个错误,你得到的只是一些 HTML 代码,说找不到该页面。

您将获得状态代码为 200 的 header 。

所以有人知道如何检查文件是否真的存在吗?

谢谢,花岗岩

最佳答案

我使用 CURL 的快速函数来执行此操作,到目前为止,即使 URL 的服务器尝试重定向,它也能正常处理:

function remoteFileExists($url){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_NOBODY, true);
$result = curl_exec($curl);
$ret = false;
if ($result !== false) {
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}

$url = "http://www.example.com";
$exists = remoteFileExists("$url/robots.txt");
if($exists){
$robottxt = file_get_contents("$url/robots.txt");
}else{
$robottxt = "none";
}

关于php - 检查文件(robots.txt,favicon.ico)到网站 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1577374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com