PHP - 如何检查 URL 是否有 404/超时?

发布于 2024-09-18 09:01:21 字数 340 浏览 2 评论 0原文

这是我的结构:

MYSQL: 表: 玩具 --->列:id、URL。如何让我的 PHP 脚本检查所有这些 URL 以查看它们是否有效或是否有 404 页?尽量不要在页面上回显或显示结果。我需要在 MYSQL 中记录一个额外的列“checks”。

结果将采用以下格式:

http://asdasd.adas --- up --- 404

它将位于如果可能的话,PHP/Curl。我已经尝试了很多年了。我放弃了所以决定在这里问。

URL 全部位于我的数据库中。

Here is my structure:

MYSQL: Table: toys ---> Column: id, URL. How do I get my PHP script to check all of those URLs to see if they are alive or have page 404's? Try not to echo or diplay the results on page. I will need to to record in MYSQL with a extra column "checks".

Results will be in this format:

http://asdasd.adas --- up --- 404

It will be in PHP/Curl if possible. I have been trying for ages. I gave up so decided to ask here.

URL's are all located in my database.

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

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

发布评论

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

评论(2

难以启齿的温柔 2024-09-25 09:01:21

在 cURL 中,有 curl_getinfo 函数,返回有关当前句柄的一些信息:

<?php
// Create a curl handle
$ch = curl_init('http://www.yahoo.com/');

// Execute
curl_exec($ch);

//fill here the error/timeout checks.

$http_code = curl_getinfo($ch,  CURLINFO_HTTP_CODE);

In cURL, there's the curl_getinfo function, that returns some info about the current handle:

<?php
// Create a curl handle
$ch = curl_init('http://www.yahoo.com/');

// Execute
curl_exec($ch);

//fill here the error/timeout checks.

$http_code = curl_getinfo($ch,  CURLINFO_HTTP_CODE);
中性美 2024-09-25 09:01:21

我相信您能够运行 SQL 查询并枚举结果,因此这是 cURL 部分。对于每个 URL,向其发送 HEAD 请求,并检查结果代码。

<?php
$handle = curl_init($yourURL);
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_exec($handle);
$result = curl_getinfo($handle, CURLINFO_HTTP_CODE);
// $result now contains the HTTP result code the page sent
?>

I trust you're able to run a SQL query and enumerate through the results, so here's the cURL part. For each URL, send it a HEAD request, and check the result code.

<?php
$handle = curl_init($yourURL);
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_exec($handle);
$result = curl_getinfo($handle, CURLINFO_HTTP_CODE);
// $result now contains the HTTP result code the page sent
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文