Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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 Yii2请求放置未正常工作_Php_Yii2_Yii2 Advanced App_Yii2 Basic App - Fatal编程技术网

Php Yii2请求放置未正常工作

Php Yii2请求放置未正常工作,php,yii2,yii2-advanced-app,yii2-basic-app,Php,Yii2,Yii2 Advanced App,Yii2 Basic App,我正在yii2中使用rest api,带有授权:承载,我的更新操作需要使用PUT发送数据。我已完全配置了操作更新,但不知何故,我在请求放置中没有获得任何数据 我在网上找到了几篇关于yii2put问题的文章,但没有找到解决方法 其中一篇文章或一个问题是,它指向了这一点 Ad如果还没有解决方案,那么我应该使用什么替代方案来执行更新操作 这是我的actionUpdatecode public function actionUpdate($id) { $params = Yii::$app-&g

我正在yii2中使用rest api,带有
授权:承载
,我的
更新
操作需要使用
PUT
发送数据。我已完全配置了
操作更新
,但不知何故,我在
请求放置
中没有获得任何数据

我在网上找到了几篇关于
yii2put
问题的文章,但没有找到解决方法

其中一篇文章或一个问题是,它指向了这一点

Ad如果还没有解决方案,那么我应该使用什么替代方案来执行
更新
操作

这是我的
actionUpdate
code

public function actionUpdate($id)
{
    $params = Yii::$app->request->bodyParams;

    $model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id' => Yii::$app->user->id])->one();

    if($model !== null){

        $model->load($params, '');
        $model->partner_id = Yii::$app->user->id;
        $model->updated_date = time();

        if ($model->save()) {

            $this->setHeader(200);
            echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);

        }
    }
 }
这是调试屏幕的截图。请参阅
事件名称
属性

这是执行
$model->load($params,')
行后的屏幕截图

我正在像下面那样调用服务,无法正确地更新数据。通过邮递员我的服务很好。所以我想我在
CURL
请求中遗漏了一些东西

$service_url = 'http://localhost/site-api/api/web/v1/events/'.$eventDetailDBI->gv ('id');
            $curl = curl_init($service_url);
            $curl_post_data = array(
                "event_name" => $eventDetailDBI->gv ('name'),

            );

            $header = array();
            $header[] = 'Authorization: Bearer 4p9mj82PTl1BWSya7bfpU_Nm';
            $header[] = 'Content-Type: application/x-www-form-urlencoded';

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
            curl_setopt($curl, CURLOPT_PUT, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
            $curl_response = curl_exec($curl);
            $json = json_decode($curl_response, true);
            curl_close($curl);
我在我的
POST
字段中获得了正确的数据,并传递了正确的数据,但服务没有更新任何数据

谢谢

试试这个:

public function actionUpdate($id)
{
    // this will get what you did send as application/x-www-form-urlencoded params
    // note that if you are sending data as query params you can use Yii::$app->request->queryParams instead.
    $params = Yii::$app->request->bodyParams;

    $model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id' => Yii::$app->user->id])->one();

    if($model !== null){

        // This will load data to your safe attribute as defined in your model rules using your default scenario.
        $model->load($params, '');

        $model->partner_id = Yii::$app->user->id;
        $model->updated_date = time();

        if ($model->save()) {

            /*
                you can use Yii::$app->getResponse()->setStatusCode(200) here but no need to do that. 
                response will be 200 by default as you are returning data.
            */

            // yii\rest\Serializer will take care here of encoding model's related attributes.
            return [
                'status' => 1,
                'data' => $model
            ];

        }
        else {
           // when validation fails. you model instance will hold error messages and response will be auto set to 422.
           return $model;
        }
    }
 }

请张贴样本代码以及。谢谢,有什么问题吗?我不知道他们的github回购中有任何与未平仓期权相关的问题。我唯一一次没有让PUT工作是因为我的IIS服务器在默认情况下禁用了PUT动词,而与Yii无关。你能通过分享代码或链接这些文章中的任何一篇来指定PUT问题吗?它在某种程度上起作用。我确实在
$params
中看到了参数,但其中也包含许多其他数据,因此我不知道如何仅在其中加载模型属性
$params
具有其他值,如
WebKitFormBoundary
等,这就是它的作用。它只会从PARAMS中得到什么:您的模型::规则()考虑安全属性。换句话说,如果在模型规则()中未定义
WebKitFormBoundary
,或者至少未设置为safe,则在将数据加载到模型时,它将被忽略。有关更多详细信息,请参阅。我是从您在代码行上方的评论中得到的。但它并没有加载数据。我马上上传调试截图。我现在正在向邮递员提出请求。我知道这并不重要,但只是想让你知道它是否会影响其他方面。检查Yii REST中内置的actionUpdate在默认情况下是如何工作的可能也很有用:我考虑过使用内置的
actionUpdate
,但正如你所看到的,我想使用2
where
条件来查找所需的记录,因此我必须重写
操作更新
。如果有任何方法,我可以做它的内置功能让我知道。谢谢