OGPからタイトル情報が取れない場合はtitleタグから取得を試みる (#65)

refs #62
This commit is contained in:
shibafu 2019-01-23 00:04:10 +09:00 committed by GitHub
parent 648e171a57
commit 895e9f4b15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -24,6 +24,12 @@ class OGPResolver implements Resolver
$metadata = new Metadata();
$metadata->title = $this->findContent($xpath, '//meta[@*="og:title"]', '//meta[@*="twitter:title"]');
if (empty($metadata->title)) {
$nodes = $xpath->query('//title');
if ($nodes->length !== 0) {
$metadata->title = $nodes->item(0)->textContent;
}
}
$metadata->description = $this->findContent($xpath, '//meta[@*="og:description"]', '//meta[@*="twitter:description"]');
$metadata->image = $this->findContent($xpath, '//meta[@*="og:image"]', '//meta[@*="twitter:image"]');

View File

@ -25,4 +25,14 @@ class OGPResolverTest extends TestCase
$this->assertEquals('The Open Graph protocol enables any web page to become a rich object in a social graph.', $metadata->description);
$this->assertEquals('http://ogp.me/logo.png', $metadata->image);
}
public function testResolveTitleOnly()
{
$resolver = new OGPResolver();
$metadata = $resolver->resolve('http://example.com');
$this->assertEquals('Example Domain', $metadata->title);
$this->assertEmpty($metadata->description);
$this->assertEmpty($metadata->image);
}
}