LoginSignup
0

More than 5 years have passed since last update.

posted at

updated at

PHP Simple HTML DOM ParserがPHP7.2で動作しない時の対処法

環境

  • PHP 7.2.0
  • PHP Simple HTML DOM Parser 1.5.2

現象

以下の結果はfalseになります。

test.php

<?php

require(__DIR__. "/vendor/autoload.php");

$uri = "https://qiita.com/tnm18";
var_dump($html = @Sunra\PhpSimple\HtmlDomParser::file_get_html($uri));

対処法

str_get_html()は問題なく動作するため、最も簡単な対処法は以下のようになります。

test2.php
<?php

require(__DIR__. "/vendor/autoload.php");

$uri = "https://qiita.com/tnm18";
$html_str = @file_get_contents($uri);
var_dump($html = @Sunra\PhpSimple\HtmlDomParser::str_get_html($html_str));

PHP Simple HTML DOM Parserは非常に簡単にDOM操作を行える優れたライブラリですが、ページのサイズが600KBを超えた場合にただfalseを返すだけだったりとエラー処理も非常にシンプルです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0