1
votes

I am writing a laravel command to read in reddit comments. I am running Laravel Framework version 5.2.45. My handle method looks like the following:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class reddit extends Command
{
    /**...**/
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $str = json_decode(file_get_contents('www.reddit.com/r/all/comments.json'));
        foreach ($str as $comments) {
            // dd($comments);
            foreach ($comments->data->children as $comment) {
                if ($comment->body_html == 'You') {
                    print_r($comment);
                } else {
                    print_r($comment);
                }
            }
        }
    }
}

When executing the command I get the following error message:

[ErrorException]
file_get_contents(www.reddit.com/r/all/comments.json): failed to open stream: No such file or directory

Any suggestions what I am doing wrong?

I appreciate your replies!

UPDATE Using the following URL reddit.com/r/all/comments.json I get:

 [ErrorException]                                                                                     
  file_get_contents(reddit.com/r/all/comments.json): failed to open stream: No such file or directory  

Exception trace: () at /home/ubuntu/workspace/app/Console/Commands/reddit.php:55 Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at n/a:n/a file_get_contents() at /home/ubuntu/workspace/app/Console/Commands/reddit.php:55 App\Console\Commands\reddit->handle() at n/a:n/a call_user_func_array() at /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Container/Container.php:507 Illuminate\Container\Container->call() at /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Console/Command.php:169 Illuminate\Console\Command->execute() at /home/ubuntu/workspace/vendor/symfony/console/Command/Command.php:256 Symfony\Component\Console\Command\Command->run() at /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Console/Command.php:155 Illuminate\Console\Command->run() at /home/ubuntu/workspace/vendor/symfony/console/Application.php:794 Symfony\Component\Console\Application->doRunCommand() at /home/ubuntu/workspace/vendor/symfony/console/Application.php:186 Symfony\Component\Console\Application->doRun() at /home/ubuntu/workspace/vendor/symfony/console/Application.php:117 Symfony\Component\Console\Application->run() at /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:107 Illuminate\Foundation\Console\Kernel->handle() at /home/ubuntu/workspace/artisan:36

1
What happens when you've replace the url to [reddit.com/r/all/comments.json] (with https:// as a prefix)User 328 6227
@eeya Thx for your reply! Please see my update.Carol.Kar

1 Answers

2
votes

Try this, worked for me: There is 307 Internal Redirect on http://www.reddit.com/r/all/comments.json

enter image description here

Try this:

  print_r(file_get_contents("https://www.reddit.com/r/all/comments.json"));