整合salesforce和backendless

时间:2019-05-25 05:40:55

标签: php wordpress api salesforce backendless

我希望注册表单数据进入Salesforce Leads表。

我有一个WordPress网站,我在其中使用php代码制作了一个注册表单,每当用户注册时,表单数据就会进入Backendless users表,为此我使用了REST API和cURL。 所以现在我也希望相同的数据也可以进入Salesforce Lead表。 我尚未找到解决此问题的任何REST API解决方案,我需要REST API或cURL的代码或可以完成我的工作并将用户数据推送到Salesforce中的插件。
我可以附加一个无尾的WordPress代码以供参考

<?php

if(isset($_POST['Submit'])) 
    {

    //extract data from the post
    //set POST variabless
    $fname = $_POST['fname'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    //retrieve the data from the backendless table and check if the email is present in it.
    $service_url1 = 'https://api.backendless.com/{APP-KEY}/{REST-API}/data/Users';
    $curl1 = curl_init($service_url1);
    curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
    $curl_response1 = curl_exec($curl1);

    if ($curl_response1 === false) {
        $info1 = curl_getinfo($curl1);
        curl_close($curl1);
        die('error occured during curl exec. Additioanl info: ' . var_export($info1));
    }
    curl_close($curl1);
    //getting the array which is stored in $curl_response1, putting it in decoded and puiling out only the email field and arranging it properly.
    $decoded = json_decode($curl_response1);
    $res1 = array_column($decoded, 'email');
    $res2 = implode("', '", $res1);
    //checking if the new user email is present in the array or not.
    if (in_array($email, $res1)) 
        {
            echo"<div style='text-align:center'>You have already registered before.</div>"; 

        }       

    else
        {
            //since its not present in the backendless table we will now add it to the backendless table by POST.
            $ch = curl_init('https://api.backendless.com/{APP-KEY}/{REST-API}/data/Users');
            $data = array("fname" => $fname, "email" => $email, "password" => $password);

            $data_string = json_encode($data);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
            );
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            //execute post
            $result = curl_exec($ch);
            //close connection
            curl_close($ch);

            echo"<div style='text-align:center'>Thank you for signing up with us ".$fname.".<br> </div>";
            echo"<div style='text-align:center'>We will be sending you an email shortly.</div>";

        }

}

else
{

?>
<html>
<head>
<style> 
input[type='text'], input[type='email'], input[type='password'] { border : 0.5px solid gray; margin-bottom: 5px; }
.signup-submit {margin-top:20px;}
input[type='password'] { 
    padding-top: 8px;
    padding-bottom: 8px;
}
</style>
</head>
<body style="text-align:left;">

<div>

    <p align="center"> Please complete the following form to register as a user </p>

    <form method="post" id="signup">

        <table>
            <tr>
                <div class="col-sm-12 col-xs-12">
                    <span class="wpcf7-form-control-wrap fname">
                        <td>
                            <label>Enter Name * : </label>
                        </td>                   
                        <td>
                            <input type="text" name="fname" value="" size="15" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required wpcf7-validates-as-name required " aria-required="true" aria-invalid="false" required />
                        </td>
                    </span>
                </div>  
            </tr>


            <tr>
                <div class="col-sm-12 col-xs-12">
                    <span class="wpcf7-form-control-wrap email">
                        <td>
                            <label>Enter Email * : </label>
                        </td>                   
                        <td>
                            <input type="email" name="email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required wpcf7-validates-as-email required email" aria-required="true" aria-invalid="false" required />
                        </td>
                    </span>
                </div>  
            </tr>

                <tr>
                <div class="col-sm-12 col-xs-12">
                    <span class="wpcf7-form-control-wrap password">
                        <td>
                            <label>Enter Password * : </label>
                        </td>                   
                        <td>
                            <input type="password" name="password"  value="" id="pwinput"  minlength="8" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" autocomplete="new-password" required />

                            <input type="checkbox" onclick="myFunction()"><span class="checkboxtext">Show Password</span>
                        </td>
                        <script>
                            function myFunction() 
                            {
                                var x = document.getElementById("pwinput");
                                if (x.type === "password") 
                                    {
                                        x.type = "text";
                                    } 
                                else 
                                    {
                                        x.type = "password";
                                    }
                            }
                        </script>
                    </span>
                </div>  
            </tr>


            <tr>
                <div class="col-sm-12 col-xs-12">

                        <td>
                            <input type="submit" value="Submit" name="Submit" class="wpcf7-form-control wpcf7-submit signup-submit center Submit">
                        </td>
                </div>  
            </tr>
        </table>  
    </form>
</div>
</body>
</html>

<?php
 }
?>

2 个答案:

答案 0 :(得分:0)

基本

Salesforce提供Web-to-Lead和Web-to-Case解决方案。它们非常易于使用(您无需先登录SF),只需单击SF即可生成HTTP POST表单。然后查看它,您可以轻松地弄清楚如何发出POST请求。

它们也是非常原始的,它们通过将您唯一的SF组织ID传递到将分派请求的通用URL来工作-因此,您肯定想隐藏服务器端,也许要用CAPTCHA保护...有人了解了您的SF组织编号,他们可以轻松地用成千上万的假潜在客户向您发送垃圾邮件。阅读与Web-to-Lead相关的帮助,限制并确定听起来是否正确。

enter image description here

SF将从中生成HTML表单

<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">

<input type=hidden name="oid" value="00D7000000{hidden}">
<input type=hidden name="retURL" value="https://stackoverflow.com">

<label for="first_name">First Name</label><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>

<label for="last_name">Last Name</label><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>
...

因此,现在您具有用于POST的URL和参数。拍上Content-Type:application/x-www-form-urlencoded标头可以很好地解决问题,您很高兴。也许可以将组织ID保留在某些配置中,以便您可以轻松地在测试和生产SF实例之间进行切换。

高级

如果您需要更高级的东西(不仅加载Lead,还加载其他表,也许是附件,也许检查是否存在相同的电子邮件,并且应该首先更新...)-您需要适当的API访问权限,SOAP或REST。这意味着两个POST调用,一个用于获取会话ID,一个用于实际保存。您可以全部手工制作,也可以使用https://developer.salesforce.com/index.php?title=Getting_Started_with_the_Force.com_Toolkit_for_PHP&oldid=51397之类的SF PHP库之一。如果您是手工制作的,则示例登录消息在此处https://stackoverflow.com/a/56034159/313628或转到REST API文档。

一旦进入(您也可以使用https://workbench.developerforce.com/login.php-> REST Explorer或https://developer.salesforce.com/docs/api-explorer/sobject/Lead

enter image description here

答案 1 :(得分:0)

您好,我正在发布对我有用的解决方案,以便将来帮助某个人!

<?php

if(isset($_POST['Submit'])) 
    {

    //extract data from the post
    //set POST variabless
    $fname = $_POST['fname'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    //retrieve the data from the backendless table and check if the email is present in it.
    $service_url1 = 'https://api.backendless.com/<App-id>/<Rest-api-key>/data/Users';
    $curl1 = curl_init($service_url1);
    curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
    $curl_response1 = curl_exec($curl1);

    if ($curl_response1 === false) {
        $info1 = curl_getinfo($curl1);
        curl_close($curl1);
        die('error occured during curl exec. Additioanl info: ' . var_export($info1));
    }
    curl_close($curl1);
    //getting the array which is stored in $curl_response1, putting it in decoded and pulling out only the email field and arranging it properly.
    $decoded = json_decode($curl_response1);
    $res1 = array_column($decoded, 'email');
    $res2 = implode("', '", $res1);

    //checking if the new user email is present in the array or not.
    if (in_array($email, $res1)) 
        {
            echo"<div style='text-align:center'>You have already registered before.</div>"; 
            echo '<th><strong><u><center><a target="_blank" href="javascript:window.close();">Click here to return.</a></center></u></strong></th>';        
        }       

    else
        {
            //since its not present in the backendless table we will now add it to the backendless table by POST.
            $ch = curl_init('https://api.backendless.com/<App-id>/<Rest-api-key>/data/Users');
            $data = array("fname" => $fname, "email" => $email, "password" => $password);

            $data_string = json_encode($data);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
            );
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            //execute post
            $result = curl_exec($ch);
            //close connection
            curl_close($ch);

            echo"<div style='text-align:center'>Thank you for signing up with us ".$fname.".<br> </div>";
            echo"<div style='text-align:center'>We will be sending you an email shortly.</div>";
            echo '<th><strong><u><center><a style="color:#2c666d" target="_blank" href="javascript:window.close();">Click here to return.</a></center></u></th>';
        }

}

else
{

?>
<html>
<head>
<style> 
input[type='text'], input[type='email'], input[type='password'] { border : 0.5px solid gray; margin-bottom: 5px; }
.signup-submit {margin-top:20px;}
input[type='password'] { 
    padding-top: 8px;
    padding-bottom: 8px;
}
</style>
</head>
<body style="text-align:left;">

<div>

    <p align="center"> Please complete the following form to register as a user </p>

    <form method="post" id="signup">

        <table>
            <tr>
                <div>
                    <span class="fname">
                        <td>
                            <label>Enter Name * : </label>
                        </td>                   
                        <td>
                            <input type="text" name="fname" value="" size="15" required />
                        </td>
                    </span>
                </div>  
            </tr>


            <tr>
                <div>
                    <span class="email">
                        <td>
                            <label>Enter Email * : </label>
                        </td>                   
                        <td>
                            <input type="email" name="email" value="" size="40" required />
                        </td>
                    </span>
                </div>  
            </tr>

                <tr>
                <div>
                    <span class="password">
                        <td>
                            <label>Enter Password * : </label>
                        </td>                   
                        <td>
                            <input type="password" name="password"  value="" minlength="8" required />

                            <input type="checkbox" onclick="myFunction()"><span class="checkboxtext">Show Password</span>
                        </td>
                    </span>
                </div>  
            </tr>

            <tr>
                <div>

                        <td>
                            <input type="submit" value="Submit" name="Submit" class="Submit">
                        </td>
                </div>  
            </tr>
        </table>  
    </form>
</div>
</body>
</html>

<?php
 }
?>