将contactform7 wordpress插件中的数据通过curl提交到api到db

时间:2018-03-16 20:10:12

标签: php wordpress

我的wordpress网站上有一个插件 - 这是contactform7。我还有一个名为提交的扩展/插件(这有些如何从我的联系表单获取所有信息并自动将其放入我的数据库。

在我以前使用html表单的网站上,我可以使用CURL通过api提交数据。

我正在尝试将两者合并在一起,因此它适用于contactform7,但它无法正常工作,表单没有给出响应,也没有显示错误。

任何建议都会很棒。

submissions.php的原始代码

 $additional_fields = apply_filters('wpcf7s_submission_fields', $submission['fields'], $submission['form_id']);
             if (!empty($additional_fields)) {



                 foreach ($additional_fields as $name => $value) {
                     if (!empty($value)) {
                         add_post_meta($post_id, 'wpcf7s_posted-' . $name, $value);
                     }
                 }
             }

我以前的HTML表单的原始submitform.php

<?php


$contaactcode=isset($_REQUEST['countrycode']) ? $_REQUEST['countrycode'] : '';
$countryname= isset($_REQUEST['phone']) ? $_REQUEST['phone'] : '';


$service_url = 'http://api.MYwebsite.com/addData';
$curl = curl_init($service_url);
$curl_post_data = array(

    'title' => isset($_REQUEST['title']) ? $_REQUEST['title'] : '',
    'Condidate'  => isset($_REQUEST['name']) ? $_REQUEST['name'] : '' ,
    'lastname'  => isset($_REQUEST['lastname']) ? $_REQUEST['lastname'] : '',
    'Email'  => isset($_REQUEST['email']) ? $_REQUEST['email'] : '',
    'whyvisa'  => isset($_REQUEST['whyvisa']) ? $_REQUEST['whyvisa'] : '',
    'visatype'  => isset($_REQUEST['visatype']) ? $_REQUEST['visatype'] : '',
    'date_day'  => isset($_REQUEST['day']) ? $_REQUEST['day'] : '',
    'date_month'  => isset($_REQUEST['month']) ? $_REQUEST['month'] : '',
    'date_year'  => isset($_REQUEST['year']) ? $_REQUEST['year'] : '' ,
    'Contact'  => $contaactcode." ".$countryname,
    'nationality'  => isset($_REQUEST['nationality']) ? $_REQUEST['nationality'] : '',
    'occupation'  => isset($_REQUEST['occupation']) ? $_REQUEST['occupation'] : '',
    'postalcode'  => isset($_REQUEST['postalcode']) ? $_REQUEST['postalcode'] : '',
    'destinationcountry'  =>  isset($_REQUEST['menu-country']) ? $_REQUEST['menu-country'] : '',
    'residingcountry'  => isset($_REQUEST['countrylist']) ? $_REQUEST['countrylist'] : '',
    'address'  => isset($_REQUEST['address']) ? $_REQUEST['address'] : '',
    'education'  => isset($_REQUEST['education']) ? $_REQUEST['education'] : '',
    'require_time'  => isset($_REQUEST['helpwithvisa']) ? $_REQUEST['helpwithvisa'] : '',
    'applydate'  => date("Y-m-d"),
    'add_info'  => isset($_REQUEST['massage']) ? $_REQUEST['massage'] : '',
    'StillInthisEmployment' => isset($_REQUEST['StillInthisEmployment']) ? $_REQUEST['StillInthisEmployment'] : '',
    'exprienceduration'  => isset($_REQUEST['exprienceduration']) ? $_REQUEST['exprienceduration'] : '' ,
    'leadfrom'  => isset($_REQUEST['lead']) ? $_REQUEST['lead'] : '' ,
    'apikey' => 'XXXXXXXXXXXX',
    'crmtype' => 'mainUk'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
var_dump($curl_response);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}

&GT;

更新submissions.php文件以将字段数据插入API

$service_url = 'http://MYwebsiteSITE.com/addData';
$curl = curl_init($service_url);

    $postfields = array();
        foreach ($_POST as $key => $value) {
            $postfields[] = stripslashes($key) . "=" . stripslashes(urlencode($value));
        }
        /* post data to curl in urlencoded format */
        $curl_post_data  = join("&", $postfields);
        //echo $curl_post_data;

        // You may need to add the apikey and crmtype if they are not part of form as below. Uncomment the line below. 
        $curl_post_data  = $curl_post_data.'&apikey=XXXXXXXXX&crmtype=mainUK';
        //echo $curl_post_data;


curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);

0 个答案:

没有答案