我在yii2中使用休息API Authorization : Bearer
和我的update
操作需要使用PUT
發送數據。我已經完全配置了actionUpdate
,但不知何故,我在Request PUT
中沒有收到任何數據。Yii2請求PUT無法正常工作
我在網上發現了幾篇關於Yii2 PUT
問題的文章,但是找不到天氣有沒有解決方案呢?
一個文章或發行的是github issue並將其指向這個github issue
廣告,如果沒有辦法解決比我應該用什麼替代的Update
行動呢。
這裏是我的actionUpdate
代碼
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);
}
}
}
這是調試屏幕的截圖。請參閱event_name
屬性。
這是$model->load($params,'')
線的執行之後的屏幕截圖。
我打電話的服務如下,不能Update
正確的數據。我的服務通過郵遞員工作正常。所以我想我在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
領域得到正確的數據,傳遞正確的數據,但該服務犯規更新任何數據。
謝謝
請郵寄樣本代碼。謝謝 –
哪些問題?我不知道他們的github回購中有任何開放的PUT相關問題。我沒有得到PUT工作的唯一時間是因爲我的IIS服務器默認情況下禁用了PUT動詞,並且與Yii無關。你能否通過分享代碼或鏈接任何這些文章來指定PUT問題? –