[英]Check whether a url exists or not in Shopify
如何驗證 Shopify 商店的 URL? 給定一個 URL 我怎么知道它是有效的 URL 還是找不到 404 頁面? 我正在使用 PHP。 我試過使用 PHP get_headers()
。
<?php
$getheadersvalidurlresponse= get_headers('https://test072519.myshopify.com/products/test-product1'); // VALID URL
print_r($getheadersvalidurlresponse);
$getheadersinvalidurlresponse= get_headers('https://test072519.myshopify.com/products/test-product1451'); // INVALID URL
print_r($getheadersinvalidurlresponse);
?>
但是對於有效和無效的 URL,我得到了相同的響應。
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
..............
)
我期待有效 URL 的 200 OK 狀態代碼和無效 URL 的 404 狀態代碼。
誰能幫助檢查給定的 shopify URL 是否有效使用 PHP?
提前致謝。
發生這種情況是因為 Shopify 區分了機器人請求和實際的真實請求,以避免在一定程度上拒絕服務攻擊。 為了克服這個問題,您必須指定user-agent
header 來模擬瀏覽器請求以獲取適當的 HTTP 響應。
作為改進,您可以發出HEAD
請求而不是GET
請求(因為get_headers()
默認使用 GET 請求,如examples
中所述),因為這里我們只關心響應元數據而不是響應正文。
片段:
<?php
$opts = array(
'http'=>array(
'method'=> "HEAD",
'header'=> "User-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
)
);
$headers1 = get_headers('https://test072519.myshopify.com/products/test-product1',0,stream_context_create($opts));
$headers2 = get_headers('https://test072519.myshopify.com/products/test-product1451',0,stream_context_create($opts));
echo "<pre>";
print_r($headers1);
print_r($headers2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.