webmentions/src/Webmention.php

71 lines
1.9 KiB
PHP
Raw Normal View History

<?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);
// }
$this->sendWebmention($source, "https://webmention.rocks/test/23/page");
}
private function getUrls(string $url) : array {
$page = file_get_contents($url);
$doc = new Crawler($page);
$urls = [];
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;
// }
}
}
?>