Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文件\u从yii2项目获取\u内容始终返回登录页面_Php_Yii2 - Fatal编程技术网

Php 文件\u从yii2项目获取\u内容始终返回登录页面

Php 文件\u从yii2项目获取\u内容始终返回登录页面,php,yii2,Php,Yii2,我有两个项目(A使用Yii2,B使用普通PHP) 我希望B使用文件\u get\u contents从A获取文件内容 但每次B请求A,它总是给我一个登录页面 这是我的密码 AuthController.php public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(),

我有两个项目(A使用Yii2,B使用普通PHP)

我希望B使用
文件\u get\u contents
从A获取文件内容

但每次B请求A,它总是给我一个登录页面

这是我的密码

AuthController.php

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['service'],
                'rules' => [
                    [
                        'actions' => ['service'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],

        ];
    }

    public function beforeAction($action)
    {
        if ($action->id == 'service')
            Yii::$app->controller->enableCsrfValidation = false;
        return parent::beforeAction($action);
    }


    public function actionService()
    {
        $status = 200;
        $status_header = 'HTTP/1.1 ' . $status . ' ' .       $this->_getStatusCodeMessage($status);
          $content_type="application/json; charset=utf-8";

          header($status_header);
          header('Content-type: ' . $content_type);
          header('X-Powered-By: ' . "Nintriva <nintriva.com>");
          echo json_encode(["hello","w"]);       
    }

    private function _getStatusCodeMessage($status)
  {
      $codes = Array(
      200 => 'OK',
      400 => 'Bad Request',
      401 => 'Unauthorized',
      402 => 'Payment Required',
      403 => 'Forbidden',
      404 => 'Not Found',
      500 => 'Internal Server Error',
      501 => 'Not Implemented',
      );
      return (isset($codes[$status])) ? $codes[$status] : '';
  }

}
$url = 'http://localhost/now/basic/web/index.php?r=auth/service';
$result = file_get_contents($url);
print_r($result);
有什么问题吗?
谢谢你改变规则。就这样吧

'rules' => [
                    [
                        'actions' => ['service'],
                        'allow' => true,
                    ],
                ],
移除

'roles' => ['@'],
或者加上这个

'roles' => ['?','@'],

您必须为您的操作禁用
AccessControl
过滤器…就像Double H所说的,您试图访问一个需要登录而不登录的页面。所以,当然,你每天都会得到登录页面,没有问题。若要避免这种情况,您应该禁用AccessControl(如果合适),或者在两台服务器之间实现某种其他类型的身份验证。这会导致对Null上的成员函数checkAccess()的错误调用,请尝试以下操作。它将允许您甚至不登录