1

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

2
  • What happens when you've replace the url to [reddit.com/r/all/comments.json] (with https:// as a prefix) Commented Apr 1, 2017 at 10:30
  • @eeya Thx for your reply! Please see my update. Commented Apr 1, 2017 at 10:32

1 Answer 1

2

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"));
Sign up to request clarification or add additional context in comments.

2 Comments

Any suggestions for a potential reason why http or no-http does not work?
If is not working because when you hit this url http://www.reddit.com/r/all/comments.json it redirects it to https://www.reddit.com/r/all/comments.json, so that why it is saying not content found for http://www.reddit.com/r/all/comments.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.