【发布时间】:2021-07-03 05:14:16
【问题描述】:
我正在检查 url,如果 url 状态代码为“200”,则返回“valid”,如果“invalid”为“404",
url 是重定向到某个页面 (url) 的链接,我需要检查该页面 (url) 的状态,以根据其状态代码确定其有效还是无效。
<?php
// From URL to get redirected URL
$url = 'https://www.shareasale.com/m-pr.cfm?merchantID=83483&userID=1860618&productID=916465625';
// Initialize a CURL session.
$ch = curl_init();
// Grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_URL, $url);
// Catch output (do NOT print!)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Return follow location true
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$html = curl_exec($ch);
// Getinfo or redirected URL from effective URL
$redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
// Close handle
curl_close($ch);
echo "Original URL: " . $url . "<br/> </br>";
echo "Redirected URL: " . $redirectedUrl . "<br/>";
function is_url_valid($url) {
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_exec($handle);
$httpCode = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($httpCode == 200) {
return 'valid link';
}
else {
return 'invalid link';
}
}
//
echo "<br/>".is_url_valid($redirectedUrl)."<br/>";
如您所见,上述链接的状态为 400,但仍显示“有效” 我正在使用上面的代码,有什么想法或更正吗?为了使其按预期工作? 似乎该网站有不止一个重定向 url 和脚本检查只有一个,这就是它显示有效的原因。 有什么想法可以解决吗?
这是我正在检查的链接
- https://click.linksynergy.com/link?id=GsILx6E5APM&offerid=547531.5112&type=15&murl=https%3A%2F%2Fwww.peopletree.co.uk%2Fwomen%2Fdresses%2Fanna-checked-dress
- https://click.linksynergy.com/link?id=GsILx6E5APM&offerid=330522.2335&type=15&murl=https%3A%2F%2Fwww.wearethought.com%2Fagnetha-black-floral-print-bamboo-dress-midnight-navy%2F%2392%3D1390%26142%3D198
- https://click.linksynergy.com/link?id=GsILx6E5APM&offerid=330522.752&type=15&murl=https%3A%2F%2Fwww.wearethought.com%2Fbernice-floral-tunic-dress%2F%2392%3D1273%26142%3D198
- https://click.linksynergy.com/link?id=GsILx6E5APM&offerid=330522.6863&type=15&murl=https%3A%2F%2Fwww.wearethought.com%2Fjosefa-smock-shift-dress-in-midnight-navy-hemp%2F%2392%3D1390%26142%3D208
- https://www.shareasale.com/m-pr.cfm?merchantID=16570&userID=1860618&productID=546729471
- https://www.shareasale.com/m-pr.cfm?merchantID=53661&userID=1860618&productID=680698793
- https://www.shareasale.com/m-pr.cfm?merchantID=66802&userID=1860618&productID=1186005518
- https://www.shareasale.com/m-pr.cfm?merchantID=83483&userID=1860618&productID=916465625
问题 -
例如 - 如果我检查此链接 https://www.shareasale.com/m-pr.cfm?merchantID=66802&userID=1860618&productID=1186005518 然后在浏览器中它继续 "404" 但在脚本 o/p 中它是 "200"
【问题讨论】:
-
上面的链接有状态码:302 & 重定向到状态码为 200 的新 url,我想检查结束 url(最后一个 url)。
-
$httpCode = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));- 为了安全起见,请确保它是用于比较的整数 -
感谢您的评论和建议,尽管我在输出中得到 404 作为状态码
-
@devhs - 我不确定这是否是正确的解决方案。但是我检查了上面的一些链接,它们正在管理 404 的自定义页面。作为一种快速解决方案,您可以使用“file_get_contents”获取 URL 的内容并检查“页面标题”。
-
通过“刷新”标头,我的意思是
header("Refresh:5; url=page2.php");在这种情况下curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);不遵循重定向,另一个是 元刷新 http-equiv 标头和 javascript 重定向