Skip to content

Instantly share code, notes, and snippets.

@filiptronicek
Created October 3, 2021 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filiptronicek/4ca1254a5ffb6174ac81568d76faeb8e to your computer and use it in GitHub Desktop.
Save filiptronicek/4ca1254a5ffb6174ac81568d76faeb8e to your computer and use it in GitHub Desktop.
Check if a link is accessible via IPFS in PHP
<?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