webmentions/src/EndpointParser.php

31 lines
906 B
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Lewisdale\Webmentions;
use Symfony\Contracts\HttpClient\ResponseInterface;
class EndpointParser {
public static function parse(ResponseInterface $response) : string | null
{
$headers = $response->getHeaders();
$endpoint = null;
if (isset($headers["link"])) {
$link = $headers["link"][0];
if (preg_match('/rel=("?)webmention("?)/', $link)) {
$matches = [];
preg_match('/\<(..*?)\>/', $link, $matches);
if (count($matches) > 1) {
$endpoint = $matches[1];
}
}
}
if ($endpoint && !str_contains($endpoint, "https://")) {
$res = parse_url($response->getInfo('url'));
$endpoint = $res["scheme"] . "://" . $res["host"] . $endpoint;
}
return $endpoint;
}
}