【问题标题】:Why cURL not sending my data to salesforce?为什么 cURL 不将我的数据发送给 salesforce?
【发布时间】:2015-05-05 13:36:03
【问题描述】:

我正在使用 drupal 表单来构建联系我们的表单。

我想将我的联系我们数据发送给销售人员。

我的联系我们表单包含操作“https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8”。

我想在将表单详细信息发送给 salesforce 之前对其进行验证,因此我使用了用于验证表单的 hook_validate 函数。

如果我在 hook_menu 中直接提及#action,那么它会直接将数据发布到 salesforce,但在发送之前不会对其进行验证。

所以,我正在尝试使用 cURL 向 salesforce 发送数据。

这是我的表格:

function contact_us_form($form_state){

     $form['orgid'] = array(
            '#name' => 'orgid',
            '#type' => 'hidden',
            '#value' => 'xxxxxx'
           );

     $form['retURL'] = array(
        '#name' => 'retURL',
        '#type' => 'hidden',
        '#value' => 'http://www.example.com'
       );

     $form['external'] = array(
        '#name' => 'external',
        '#type' => 'hidden',
        '#value' => '1'
       );

    $form['name'] = array(
        '#type' => 'textfield',
        '#size' => 40,
        '#id' => 'name',
        '#title' => t('First and Last Name'),
        '#prefix' => '<table><tr><td>',
        '#suffix' => '</td></tr>',
        '#required' => TRUE
       );

     $form['email'] = array(
        '#type' => 'textfield',
        '#size' => 40,
        '#id' => 'email',
        '#title' => t('Valid Email Address'),
        '#prefix' => '<tr><td>',
        '#suffix' => '</td></tr>',

       );
     $form['phone'] = array(
        '#type' => 'textfield',
        '#id' => 'phone',
        '#title' => t('Phone Number'),
        '#size' => 40,
        '#prefix' => '<tr><td>',
        '#suffix' => '</td></tr>'

    );

    $form['subject'] = array(
        '#type' => 'textfield',
        '#size' => 40,
        '#title' => t('Subject'),
        '#prefix' => '<tr><td>',
        '#suffix' => '</td></tr>'
    );

    $form['desc'] = array(
        '#type' => 'textarea',
        '#size' => 40,
        '#title' => t('Description1'),
        '#prefix' => '<tr><td>',
        '#suffix' => '</td></tr>'
    );

   $form['type'] = array(
        '#name' => 'type',
        '#type' => 'hidden',
        '#value' => 'Web-to-case'
       );

 //  // Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
    $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#prefix' => '<br><tr><td style="padding-left:150px;">',
    '#suffix' => '</td></tr></table>'
  ); 


  return $form;
}

表单正在正确重绘。以下是我的验证功能:

    function contact_us_form_validate($form, $form_state) {
   //logic to check fields.     
}

以下是我的提交代码,它不起作用: 理想情况下,cURL 调用不应该返回,因为我在表单中提到了 retURL,并且返回 URL 将由 salesforce 设置。目前我没有被重定向到 retURL。没有任何条目将进入 salesforce。

function contact_us_form_submit($form, $form_state){

  $temp = $form_state['values'];
  $ch = curl_init();
  foreach ($temp as $key => $value) {

//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($key)."=".stripslashes($value);

}
  $query_string = join("&", $kv);


//The original form action URL from Step 2 :)
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';

//Open cURL connection
$ch = curl_init();

//Set the url, number of POST vars, POST data
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POST, count($kv));
     curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
     curl_setopt($ch, CURLOPT_HEADER, FALSE);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 $result = curl_exec($ch);

//close cURL connection
curl_close($ch);

}

【问题讨论】:

  • 你看过 Guzzle 之类的东西而不是 cURL 吗?它对开发人员更加友好......只是一个想法。
  • 感谢好友对解决上述问题表现出兴趣,但在 Guzzle 上的集成需要一些时间。我没有得到上面代码中缺少的内容?
  • servlet.WebToLead 通常以 HTML 响应以将用户浏览器重定向到 retURL。您可能还会发现添加值为 1 的 debug 输入很有用。

标签: php forms curl salesforce drupal-6


【解决方案1】:

我想你想要...

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //returns response as data.

我知道这听起来很愚蠢,但是您已经检查过是否安装了 cURL 并且在 php 中启用了它吗?

您还应该尝试检查 $result 是否为假。

if($result=== false)
{
    echo "Error Number:".curl_errno($ch)."<br>";
    echo "Error String:".curl_error($ch);
}

只是看看是否有问题,例如由于某些安全问题,您无法发布 salesforce,例如您没有传递您的 salesforce.com 用户名和密码或您帐户的 API 密钥。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2012-05-19
    • 2012-09-24
    • 1970-01-01
    相关资源
    最近更新 更多