0
Help us understand the problem. What are the problem?

More than 3 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を返すだけだったりとエラー処理も非常にシンプルです。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
0
Help us understand the problem. What are the problem?