Last active
December 27, 2018 07:24
-
-
Save adactio/e9c3b157dcd865dec6ca to your computer and use it in GitHub Desktop.
Search a web page for a webmention endpoint and, if one exists, send a webmention to it.
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 | |
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication | |
# http://creativecommons.org/publicdomain/zero/1.0/ | |
function sendWebmention($source, $target) { | |
$endpoint = false; | |
$options = array( | |
CURLOPT_URL => $target, | |
CURLOPT_USERAGENT => 'adactio.com', | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_HEADER => FALSE, | |
); | |
$curl = curl_init(); | |
curl_setopt_array($curl, $options); | |
$body = curl_exec($curl); | |
curl_close($curl); | |
$dom = new \DOMDocument(); | |
@$dom -> loadHTML($body); | |
$xpath = new \DOMXPath($dom); | |
foreach ($xpath -> query('//*[local-name(.) = "a" or local-name(.) = "link"][contains(@rel, "webmention")]') as $node) { | |
$endpoint = $node -> getAttribute("href"); | |
break; | |
} | |
if (empty($endpoint)) { | |
return false; | |
} | |
if (!stristr($endpoint, '//')) { | |
$components = parse_url($target); | |
$domain = $components['scheme'].'://'.$components['host']; | |
if (substr($endpoint, 0, 1) == '/') { | |
$endpoint = $domain.$endpoint; | |
} elseif (stristr($endpoint, '../')) { | |
$path = $domain; | |
$nesting = substr_count($endpoint,'../'); | |
$nesting++; | |
$directories = explode('/', $target); | |
for ($i = 1; $i < count($directories)-$nesting; $i++) { | |
if (substr($path,0,-1) != '/') { | |
$path.= '/'; | |
} | |
$path.= $directories[$i]; | |
} | |
if (substr($path,0,-1) != '/') { | |
$path.= '/'; | |
} | |
$path.= $endpoint; | |
$path = str_replace('../','',$path); | |
$endpoint = $path; | |
} else { | |
$endpoint = $domain.'/'.$endpoint; | |
} | |
} | |
$data = array( | |
'source' => $source, | |
'target' => $target | |
); | |
$options = array( | |
CURLOPT_URL => $endpoint, | |
CURLOPT_USERAGENT => 'adactio.com', | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_POST => TRUE, | |
CURLOPT_POSTFIELDS => $data, | |
CURLOPT_TIMEOUT => 20 | |
); | |
$curl = curl_init(); | |
curl_setopt_array($curl, $options); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
return; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment