Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

環境

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

tnm18
CWを中心にフリーのエンジニアとして活動しております。 業務自動化・スクレイピングを得意としており、数多くの案件を経験してきました。 日々進化するWEB技術を取り込んで自分のスキルを上げていくことを心がけております。 お仕事のご相談もお気軽にどうぞ。
https://crowdworks.jp/public/employees/820578
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした