2023-03-08 15:31:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Lewisdale\Webmentions;
|
|
|
|
|
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
use Symfony\Component\HttpClient\HttpClient;
|
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
|
|
|
|
class Webmention {
|
|
|
|
private readonly HttpClientInterface $client;
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->client = HttpClient::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendForPage(string $source) : void {
|
|
|
|
// $urls = $this->getUrls($source);
|
|
|
|
|
|
|
|
// foreach($urls as $target) {
|
|
|
|
// $this->sendWebmention($source, $target);
|
|
|
|
// }
|
|
|
|
|
2023-03-08 21:27:47 +00:00
|
|
|
$this->sendWebmention($source, "https://webmention.rocks/test/23/page");
|
2023-03-08 15:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getUrls(string $url) : array {
|
|
|
|
$page = file_get_contents($url);
|
|
|
|
$doc = new Crawler($page);
|
|
|
|
|
2023-03-08 21:27:47 +00:00
|
|
|
$urls = [];
|
2023-03-08 15:31:49 +00:00
|
|
|
foreach ($doc->filter('.h-entry a') as $anchor) {
|
|
|
|
$target_url = $anchor->attributes->getNamedItem('href')->textContent;
|
|
|
|
|
|
|
|
if ($target_url !== null && strlen($target_url)) {
|
|
|
|
$urls[] = $target_url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sendWebmention(string $source, string $target) {
|
|
|
|
// 1. Endpoint discovery
|
|
|
|
// 1.1 Parse page
|
|
|
|
// 1.2 Look for rel="webmention"
|
|
|
|
$endpoint = $this->getWebmentionEndpoint($target);
|
|
|
|
echo ($endpoint ?? "no :(") . "<br />";
|
|
|
|
// 2. Send webmention to endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getWebmentionEndpoint(string $url) : string | null {
|
|
|
|
$response = $this->client->request('GET', $url);
|
|
|
|
// echo "<pre>";
|
|
|
|
// var_dump($response->getInfo());
|
|
|
|
// echo "</pre>";
|
|
|
|
|
|
|
|
return EndpointParser::parse($response);
|
|
|
|
|
|
|
|
// $doc = new Crawler($page);
|
|
|
|
|
|
|
|
// $links = $doc->filter('link[rel="webmention"]');
|
|
|
|
|
|
|
|
// if ($links->count()) {
|
|
|
|
// return $links->first()->attr('href');
|
|
|
|
// } else {
|
|
|
|
// return null;
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|