45 lines
1.1 KiB
PHP
45 lines
1.1 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\MetadataResolver;
|
||
|
|
||
|
use GuzzleHttp\Client;
|
||
|
|
||
|
class PlurkResolver implements Resolver
|
||
|
{
|
||
|
/**
|
||
|
* @var Client
|
||
|
*/
|
||
|
private $client;
|
||
|
/**
|
||
|
* @var OGPResolver
|
||
|
*/
|
||
|
private $ogpResolver;
|
||
|
|
||
|
public function __construct(Client $client, OGPResolver $ogpResolver)
|
||
|
{
|
||
|
$this->client = $client;
|
||
|
$this->ogpResolver = $ogpResolver;
|
||
|
}
|
||
|
|
||
|
public function resolve(string $url): Metadata
|
||
|
{
|
||
|
$res = $this->client->get($url);
|
||
|
if ($res->getStatusCode() === 200) {
|
||
|
$metadata = $this->ogpResolver->parse($res->getBody());
|
||
|
|
||
|
$dom = new \DOMDocument();
|
||
|
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
|
||
|
$xpath = new \DOMXPath($dom);
|
||
|
$imageNode = $xpath->query('//div[@class="text_holder"]/a[1]')->item(0);
|
||
|
|
||
|
if($imageNode) {
|
||
|
$metadata->image = $imageNode->getAttribute('href');
|
||
|
}
|
||
|
|
||
|
return $metadata;
|
||
|
} else {
|
||
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
||
|
}
|
||
|
}
|
||
|
}
|