示例#1
0
 /**
  * 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();
             });
         }
     }
 }
 /**
  * Enable / Disable multiple Webhooks
  *
  * @param $status
  * @return \yii\web\Response
  * @throws NotFoundHttpException
  * @throws \Exception
  */
 public function actionUpdateStatus($status)
 {
     $accounts = Webhook::findAll(['id' => Yii::$app->getRequest()->post('ids')]);
     if (empty($accounts)) {
         throw new NotFoundHttpException(Yii::t('webhooks', 'Page not found.'));
     } else {
         foreach ($accounts as $account) {
             $account->status = $status;
             $account->update();
         }
         Yii::$app->getSession()->setFlash('success', Yii::t('webhooks', 'The selected items have been successfully updated.'));
         return $this->redirect(['index']);
     }
 }