我有2个项目(A使用Yii2和B使用普通PHP)。
我希望B使用file_get_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] : '';
}
}
b.php
$url = 'http://localhost/now/basic/web/index.php?r=auth/service';
$result = file_get_contents($url);
print_r($result);
有什么不对吗? 三江源
答案 0 :(得分:3)
更改规则。像这样做
'rules' => [
[
'actions' => ['service'],
'allow' => true,
],
],
删除
'roles' => ['@'],
或只是添加此
'roles' => ['?','@'],