Created
October 3, 2021 11:07
-
-
Save filiptronicek/4ca1254a5ffb6174ac81568d76faeb8e to your computer and use it in GitHub Desktop.
Check if a link is accessible via IPFS in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Checks if a given `$url` is hosted on IPFS | |
* | |
* @param string $url | |
* @return bool | |
*/ | |
function checkIPFS($url) | |
{ | |
$headers = get_headers($url); | |
$ipfs = false; | |
$protocol = parse_url($url, PHP_URL_SCHEME); | |
if ($protocol !== "ipfs") { | |
foreach ($headers as $key => $value) { | |
if (is_array($value)) { | |
$value = end($value); | |
} | |
$headerParts = explode(":", $value); | |
if ($headerParts[0] === "X-Ipfs-Path") { | |
$ipfs = true; | |
break; | |
} | |
} | |
} else { | |
$ipfs = true; | |
} | |
return $ipfs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment