Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gamezen's avatar

error when scraping data from site

i want scrap data from other site to my create post blade, im use voku/simple_html_dom. when i try i get this error voku\helper\HtmlDomParser::loadHtml(): Argument #1 ($html) must be of type string, bool given

<?php

namespace App\Services;

use voku\helper\HtmlDomParser;

class Scrap
{
    private $baseTarget = 'website';

    private function init($path)
    {
        $curl = curl_init($this->baseTarget . $path);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        $html = curl_exec($curl);
        curl_close($curl);

        return HtmlDomParser::str_get_html($html);
    }

    public function getData()
    {
        $html = $this->init('/blog/howtoinstalllaravel');

        // Wrapper
        $wrapper = $html->findOne('#content');

        // Get Title
        $data['title'] = $wrapper->findOne('.lead')->innerText();

        return $data;
    }
}
0 likes
2 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60

https://www.php.net/manual/en/function.curl-exec.php

Returns true on success or false on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, false on failure.

$html = curl_exec($curl);

if (! $html) { // failed
	return 'Something went wrong';
}

return HtmlDomParser::str_get_html($html);
gamezen's avatar

@Sergiu17 I tried another website for scraping and it worked, can't all websites be scraping?

Please or to participate in this conversation.