2017-04-01 25 views
1

我正在寫一篇laravel命令來閱讀reddit註釋。我正在運行Laravel Framework version 5.2.45。我把手方法如下所示:通過laravel命令獲取json - 無法打開流

<?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):未能打開 流:沒有這樣的文件或目錄

任何建議我做錯了嗎?

我很感謝你的回覆!

UPDATE 使用下列URL 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 

異常跟蹤: ()在/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()在n/a:n/a call_user_func_array()位於/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Container/Container.php:507 照亮\ Container \ Container-> call()位於/ home/ubuntu/workspace/vendor /照亮\ Console \ Command-> execute()at /home/ubuntu/workspace/vendor/symfony/console/Command/Command.php:256 Symfony \在/ home/subuntu/workspace /vendor/laravel/framework/src/Illuminate/Console/Command.php:155 上點亮\ Console \ Command-> run() 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()位於/ home/ubuntu/workspa在/ home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel中運行()。 PHP:107 照亮\基金會\控制檯\內核級>手柄()通過/ home/Ubuntu的/工作區/工匠:36

+0

將網址替換爲[https://www.reddit.com/r/all/comments.json](以https://爲前綴)時會發生什麼 – eeya

+0

@eeya Thx for your回覆!請參閱我的更新。 – mrquad

回答

2

試試這個,工作對我來說:有307 Internal Redirecthttp://www.reddit.com/r/all/comments.json

enter image description here

試試這個:

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

Thx爲您的答覆! 「https」似乎工作。 – mrquad

+0

@mrquad歡迎... :) –

+0

有關爲什麼'http'或'no-http'不起作用的潛在原因的任何建議? – mrquad