S
S
SevenShots2019-09-12 16:50:57
PHP
SevenShots, 2019-09-12 16:50:57

Webhook not sending from Wordpress?

You need to send a webhook via curl to wordpress to add requests to AmoCRM.
For this, I added code to the function file in Wordpress

/ Вызываем функцию для перехвата данных
function your_wpcf7_mail_sent_function($contact_form){

    // Перехватываем данные из Contact Form 7
    $title = $contact_form->title;
    $posted_data = $contact_form->posted_data;

    //Вместо "Контактная форма 1" необходимо указать название вашей контактной формы
    if ('Записаться в салон' == $title) {
        $submission = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();

        // Далее перехватываем введенные данные в полях Contact Form 7:
        // 1. Перехватываем поле [your-name] - Имя
        $AmoCRM['name'] = $posted_data['your-name'];
        // 2. Перехватываем поле [your-message] - Телефон
        $AmoCRM['phone'] = $posted_data['your-phone'];
    }
    if ('Подобрать программу' == $title) {
        $submission = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();

        // Далее перехватываем введенные данные в полях Contact Form 7:
        // 1. Перехватываем поле [your-name] - Имя
        $AmoCRM['name'] = $posted_data['your-name'];
        // 2. Перехватываем поле [your-message] - Телефон
        $AmoCRM['phone'] = $posted_data['your-phone'];

    }
    if ('Узнать больше'== $title) {
        $submission = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();

        // Далее перехватываем введенные данные в полях Contact Form 7:
        // 1. Перехватываем поле [your-name] - Имя
        $AmoCRM['name'] = $posted_data['your-name'];
        // 2. Перехватываем поле [your-message] - Телефон
        $AmoCRM['phone'] = $posted_data['your-phone'];
    }

    $AmoCRM['cookie'] = $_COOKIE; #Добавляем куки с UTM в массив

    $post_data = array(
        "contact" => array(
            "phone" => $AmoCRM['phone'], #Телефон клиента
            "name" => $AmoCRM['name'], #Имя клиента
            "email" => "[email protected]", #Email клиента
            "message" => "Новая сборка интеграции AmoCRM", #Сообщение от клиента
        ),
    );
    
    $url_hook = "url сайта";
    if ($AmoCRM['phone'] != "") {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_hook);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 5);
        // указываем, что у нас POST запрос
        curl_setopt($ch, CURLOPT_POST, 1);
        // добавляем переменные
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $post_data = json_encode($post_data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($post_data)));

        $output_amo = curl_exec($ch);
        curl_close($ch);

    }
    



}
add_action( 'wpcf7_mail_sent', 'your_wpcf7_mail_sent_function' );

Which quietly throws a hook on the webhook.site
tester. But it is not possible to accept it and run the script on the processor. Though if I start from other server, everything works. Here is the code itself:
#Принимаем Вебхук
$post_data = file_get_contents('php://input'); #Принимаем webhook
$post_data = json_decode($post_data, true); #Раскодируем из json

http_response_code(200); #Отправка положительного ответа

And then we process it ... But for some reason the hook does not come there. What could be the reason?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question