Check whether a URL exists or not

Highlighted
Excursionist
59 0 1

How can I validate a Shopify store's URL? Given a URL how can I know whether it is a valid URL or 404 page not found? I'm using PHP. I've tried using PHP get_headers().

<?php
$getheadersvalidurlresponse= get_headers('https://testxxxx.myshopify.com/products/test-product1'); // VALID URL
print_r($getheadersvalidurlresponse);

$getheadersinvalidurlresponse= get_headers('https://testxxxx.myshopify.com/products/test-product1451'); // INVALID URL
print_r($getheadersinvalidurlresponse); 
?>

But for both valid and invalid URLs, I got the same response.

Array
(
    [0] => HTTP/1.1 403 Forbidden
    [1] => Date: Wed, 08 Jul 2020 13:27:52 GMT
    [2] => Content-Type: text/html
    [3] => Connection: close
   ..............
)

I'm expecting 200 OK status code for valid URL and 404 for invalid URL. 

Can anyone please help to check whether given shopify URL is valid or not using PHP?

Thanks in advance.

 

0 Likes
Shopify Partner
530 38 109

Must be something in how the PHP get_headers function requests things. While I'm not well-versed in PHP, the Javascript code below works fine. It requests not just the headers but pulls the page. If the page pull fails then that's handled.

 

$.get("https://{my_shop}.myshopify.com/pages/{my_page}").done(function () {
  alert("success");
}).fail(function () {
   alert("failed.");
});
1 Like