/** * Send the form submission data through an HTTP POST request * either as URL encoded form data or as a JSON string * depending on the format selected in the Webhook configuration * * @param $event */ public function sendSubmissionData($event) { if (isset($event, $event->form, $event->form->id, $event->submission)) { $webhooks = Webhook::findAll(['form_id' => $event->form->id, 'status' => 1]); $client = new Client(); $body = $event->submission->getSubmissionData(); foreach ($webhooks as $webhook) { // Add Form ID, Form Name and IP Address $body = $body + ['form_id' => $event->form->id, 'form_name' => isset($event->form->name) ? Html::encode($event->form->name) : '', 'ip_address' => Yii::$app->request->getUserIP()]; // Add Handshake Key if (!empty($webhook->handshake_key)) { $body = $body + ['handshake_key' => $webhook->handshake_key]; } // Add Json Format if ($webhook->json === 1) { $body = Json::encode($body); } // Send HTTP POST request asynchronously $response = $client->post($webhook->url, ['future' => true, 'headers' => ['User-Agent' => Yii::$app->name], 'body' => $body, 'allow_redirects' => false, 'timeout' => 5]); // Call the function when the response completes $response->then(function ($response) { // echo $response->getStatusCode(); }); } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Webhook::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'form_id' => $this->form_id, 'status' => $this->status, 'json' => $this->json]); $query->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'handshake_key', $this->handshake_key]); return $dataProvider; }
/** * Finds the Webhook model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Webhook the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Webhook::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }