I am checking for url & return "valid" if url status code "200" & "invalid" if its on "404",
urls are links which redirect to a certain page (url) & i need to check that page (url) status to determine if its valid or invalid on the basis of its status code.
<?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/>";
As you can see the above link has status 400 still it shows "valid" I am using above code, any thoughts or correction's ? in order to make it work as expected ? It seems like the site has more then one redirected url & script checks for only one that's why it shows valid. any thoughts how to fix it ?
Here are the links which i am checking with
- 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
ISSUE -
FOR EXAMPLE - If i check with this link https://www.shareasale.com/m-pr.cfm?merchantID=66802&userID=1860618&productID=1186005518 then in browser it goes on "404" but in script o/p its "200"