我正在检查URL并返回“有效”,如果URL状态代码为“200”,并且如果URL在“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",但在脚本输出中是"200"
header("Refresh:5; url=page2.php");
,而curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
则不会跟随重定向,另一个是meta refresh http-equiv header和javascript重定向。 - Haridarshan