gpt4 book ai didi

php - 我想检查此 cURL 代码中的站点是否存在?

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

我使用此代码从其他服务器获取响应/结果,我想知道如何检查该站点是否存在?

$ch = curl_init('http://domain.com/curl.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
if (!$result)
// it will execute some codes if there is no result echoed from curl.php

最佳答案

您真正需要做的就是一个 HEAD 请求,以查看您是否在重定向后收到 200 OK 消息。 你不需要为此做一个完整的 body 请求。事实上,你根本不应该。

function check_alive($url, $timeout = 10) {
$ch = curl_init($url);

// Set request options
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_NOBODY => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_USERAGENT => "page-check/1.0"
));

// Execute request
curl_exec($ch);

// Check if an error occurred
if(curl_errno($ch)) {
curl_close($ch);
return false;
}

// Get HTTP response code
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Page is alive if 200 OK is received
return $code === 200;
}

关于php - 我想检查此 cURL 代码中的站点是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6541785/

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