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

glena072's avatar

returning api array from files in a directory

Hi All,

hope everyone is having a great weekend.

I am fairly new to laravel and having a little trouble with an API response.

I have a project that requires me to take a directory of ebooks and load them to a database, then return a view with a cover card and a single book view with the book details

I can use the Illuminate\Support\Facades\Http; to return the data manually through a controller and save the required details from the API to my database, then rendered the data to my view.

now I am trying to use the file name in the directory to achieve the same result dynamically, but seem to be running into a few roadblocks. I have been able to output all the file names in the directory to a single page, now i want to grab that file name and send it to the open library API to retrieve the book's data but I am getting a time out.

If I remove the Http request from the for each collection method I return my files in the directory, but when I add it back in I get my time out.

here is my file method in my controller.

public function files()

{
    $dir_files = File::files(storage_path('app/public/ebooks'));

    $array = [];
    foreach ($dir_files as $file){
        $array[] = $file->getFilename();
    }

    $book_array = collect($array);

    $book_array->each(function ($file, $key) {
        
        $file = Str::before($file, '.epub');
        $filename = Http::get('http://openlibrary.org/search.json?q='.$file)->json('docs');
        return $filename;
    })->count();

    return view('files.index', [

    'book_files' => $book_array
    ]);
}

any advice would be appreciated.

thanks, Glen.

0 likes
3 replies
glena072's avatar

@tisuchi thanks for the reply. No i haven’t looked at timeout() or retry(). I’ll look at those.

glena072's avatar

@tisuchi i resolved the issue, i needed to use the withOptions()->withHeaders() methods.

thank you.

Please or to participate in this conversation.