通过laravel命令获取json - 无法打开流

时间:2017-04-01 10:26:56

标签: php json laravel-5

我正在写一个laravel命令来读取reddit注释。我正在运行Laravel Framework version 5.2.45。我的句柄方法如下所示:

<?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);
                }
            }
        }
    }
}

执行命令时,我收到以下错误消息:

  

[ErrorException]
  file_get_contents(www.reddit.com/r/all/comments.json):无法打开   stream:没有这样的文件或目录

有什么建议我做错了吗?

感谢您的回复!

更新 使用以下网址reddit.com/r/all/comments.json,我得到:

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

异常追踪:        ()athome/ubuntu/workspace/app/Console/Commands/reddit.php:55        在n / a上照亮\ Foundation \ Bootstrap \ HandleExceptions-&gt; handleError():不适用        /home/ubuntu/workspace/app/Console/Commands/reddit.php:55中的file_get_contents()        app / Console \ Commands \ reddit-&gt;句柄()在n / a:n / a        call_user_func_array()athome/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Container/Container.php:507        在/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Console/Command.php:169照亮\ Container \ Container-&gt; call()        在/home/ubuntu/workspace/vendor/symfony/console/Command/Command.php:256照亮\ Console \ Command-&gt; execute()        Symfony \ Component \ Console \ Command \ Command-&gt;运行()/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Console/Command.php:155        在/home/ubuntu/workspace/vendor/symfony/console/Application.php:794照亮\控制台\命令 - &gt; run()        Symfony \ Component \ Console \ Application-&gt; doRunCommand()/home/ubuntu/workspace/vendor/symfony/console/Application.php:186        Symfony \ Component \ Console \ Application-&gt; doRun()athome/ubuntu/workspace/vendor/symfony/console/Application.php:117        Symfony \ Component \ Console \ Application-&gt; run()athome/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:107        Illuminate \ Foundation \ Console \ Kernel-&gt; handle()at / home / ubuntu / workspace / artisan:36

1 个答案:

答案 0 :(得分:2)

试试这个,为我工作:307 Internal Redirect上有http://www.reddit.com/r/all/comments.json

enter image description here

试试这个:

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