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

helpmyworld's avatar

foreach() argument must be of type array|object, string given

Please help with the below error

ErrorException HTTP 500 Internal Server Error foreach() argument must be of type array|object, string given ErrorException Show exception properties ErrorException {#1531 ▼ #severity: E_WARNING


Route::get('gallery', [App\Http\Controllers\GalleryController::class, 'index'])->name('gallery');			

//view

@foreach ($images as $
       <div class="properties-img">
            <a href="pro-details.html"><img src="assets/img/gallery/latest5.jpg" alt=""></a>
                  <img src="{{url('insta/images/')}}/{{$key.'.png'}}" alt=" " class="img-responsive">
                </div>
@endforeach

//Controller


<?php
namespace App\Http\Controllers;
class GalleryController extends Controller
{
    /**
     * This function show the image from instagram.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'Password#', new Psr16Adapter('Files'));
        $instagram->login(); // will use cached session if you want to force login $instagram->login(true)
        $instagram->saveSession();  //DO NOT forget this in order to save the session, otherwise have no sense
        $account = $instagram->getAccount('username');
        $accountMedias = $account->getMedias(); 
        foreach ($accountMedias as $key  => $accountMedia) {
            $images[$key] = str_replace("&amp;","&", $accountMedia->getimageHighResolutionUrl());     
            $path = $images[$key];
            $imageName = $key.'.png';
            $img = public_path('insta/images/') . $imageName;
            file_put_contents($img, file_get_contents($path));
        }
        return view('gallery', compact('images'));
    }
}

// From the line that the error says it is

private static function parseCookies($rawCookies)
    {
        $cookies = [];
        foreach ($rawCookies as $c) {
            $c = explode(';', $c)[0];
            $parts = explode('=', $c);
            $cookies[$parts[0]] = $parts[1];
        }
        return $cookies;
    }
0 likes
3 replies
tykus's avatar

What is calling parseCookies? And what is $rawCookies?

helpmyworld's avatar

@tykus I won't lie I don't know. This was supposed to be a straight forward thing because the tutorial I did i just required to run composer to install the plugin and populate the controller and then pull the images from my Instagram account

tykus's avatar

@helpmyworld it likely will be apparent in the stack trace from the error; or in your application logs.

Please or to participate in this conversation.