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

Jacobs's avatar

Curl, Guzzle, file_get_contents all return empty string

Hey, parsing a town info page while also trying to download an image of it's blazon, however neither of my attempts was successful, the links are correct as there's a .gif on there.

$towns is an array of arrays, each containing town name and full link for more info, link is correct as I have no issue parsing all the data including the image url, but I am unable to download the image from the source.

allow_url_fopen is on, open_ssl is on

Function:

    private function getSpecificTownData($towns) {
        $data = [];
        $info = [];
        $i = 0;
        //foreach($towns as $town)
        while($i < 20) {
            $html = HtmlDomParser::file_get_html($towns[$i]['href'], false, null, 0);
            //get parent table
            $parent_table = $html->find('td[height=29]', 0);
            //first child table contains pic, address, phone, email, web)
            $table[0] = $parent_table->find('table', 0);
            //third child td contains img url, fax
            $third = $table[0]->find('tbody > tr', 3);

            $imgUrl = $third->children(0)->children(0)->getAttribute('src');
            $img = file_get_contents($imgUrl);
            $imgName = basename($imgUrl);
            $this->info($img); //keeps printing empty row => contains empty string
            Storage::put($imgName, $img);

            $fax = $third->children(3)->find('table > tbody > tr > td', 0)->plaintext;
            //4th child td contains email, web, street, address
            $fourth = $table[0]->find('tbody > tr', 6);
            $street = $fourth->children(0)->plaintext;
            $email = $fourth->children(2)->plaintext;
            //5th child td contains address, website
            $fifth = $table[0]->find('tbody > tr', 7);
            $address = $fifth->children(0)->plaintext;
            $web = $fifth->children(2)->plaintext;
            //second child table contains phone, mayor name
            $table[1] = $parent_table->find('table', 3);
            if($tr = $table[1]->find('tbody > tr', 8)) {
                $phone = $tr->children(1)->plaintext;
            }
            else {
                $phone = null;
            }
            $mayor = $table[1]->find('tbody > tr', 7)->children(1)->plaintext;

            //get municipal office lat + lon
            $response = Geocode::make()->address($street . ', ' . $address);
            if($response) {
                $coords = $response->latitude() . ', ' . $response->longitude();
            }
            else {
                $coords = 'Neznáma';
            }
            $info['name'] = $towns[$i]['name'];
            $info['mayor'] = $mayor;
            $info['address'] = $street . ', ' . $address;
            $info['phone'] = $phone;
            $info['fax'] = $fax;
            $info['email'] = $email;
            $info['website'] = $web;
            $info['office'] = $coords;
            $info['image'] = $imgName;

            $data[$i++] = $info;
        }
        return $data;
    }

If printing also urls to check if they're okay:

https://www.e-obce.sk/erb/1.gif
https://www.e-obce.sk/erb/2.gif
https://www.e-obce.sk/erb/3.gif
https://www.e-obce.sk/erb/4.gif
https://www.e-obce.sk/erb/5.gif
https://www.e-obce.sk/erb/6.gif
https://www.e-obce.sk/erb/7.gif
https://www.e-obce.sk/erb/8.gif
https://www.e-obce.sk/erb/9.gif
https://www.e-obce.sk/erb/10.gif
https://www.e-obce.sk/erb/11.gif
https://www.e-obce.sk/erb/12.gif
https://www.e-obce.sk/erb/13.gif
https://www.e-obce.sk/erb/14.gif
https://www.e-obce.sk/erb/15.gif
https://www.e-obce.sk/erb/16.gif
https://www.e-obce.sk/erb/17.gif
https://www.e-obce.sk/erb/18.gif
https://www.e-obce.sk/erb/19.gif
https://www.e-obce.sk/erb/20.gif
0 likes
2 replies
Snapey's avatar

check that basename($imgUrl); produces a full server path to a location that you can write to.

What is $this->info()

Jacobs's avatar
Jacobs
OP
Best Answer
Level 2

Actually it's working properly but due to the download speed it kinda doesn't have the time to print it.

$this->info is printing to console as this is a function inside a custom artisan command.

All good, works flawlessly.

Please or to participate in this conversation.