Yii2控制台从外部服务器获取数据

I am creating a console application where I would like to fetch data from another url whenever the command is run

This is how I have implemented the console controller

<?php
  namespace console\controllers;

  use yii\helpers\Console;
  use yii\console\Controller;
   ... other use imports
   use Yii;


class UserController extends Controller
{

  public function actionInit()
   {
     $urltofetchdata = "https://urltofetchjsondata"; //i expect to return json

     $datas= //how do i get the data here so that i can proceedby

      foreach($datas as $data){
        $user = new User();
        $user->name = $data->name;
        $user->email = $data->email;
        $user->save();

    }
   }
  }

so that when a user types:

./yii user/init 

the data can be retrieved.

if allow_url_fopen is activated on your server you can use file_get_contents to grab the data remotely; something like this,

public function actionInit()
{
    $urltofetchdata = "https://urltofetchjsondata"; //i expect to return json
    $datas = json_decode(file_get_contents($urltofetchdata));

    foreach($datas as $data) {
        $user = new User();
        $user->name = $data->name;
        $user->email = $data->email;
        $user->save();
    }
}

if allow_url_fopen is disabled on your server, you can use cURL

public function actionInit()
{
    $urltofetchdata = "https://urltofetchjsondata"; //i expect to return json

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $urltofetchdata);
    $result = curl_exec($ch);
    curl_close($ch);

    $datas = json_decode($result);

    foreach($datas as $data) {
        $user = new User();
        $user->name = $data->name;
        $user->email = $data->email;
        $user->save();
    }
}