doudou7361 2015-05-05 13:36
浏览 81

为什么cURL不将数据发送给salesforce?

I am using drupal form to build a contact us form.

I want to send my contact us data to salesforce.

My contact us form has action "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8".

I want validate the form details before it is sent to salesforce so I am using hook_validate function which validates the form.

If I directly mention #action in hook_menu then it directly posts data to salesforce but doesn't validates it before sending.

SO, I am trying to use cURL to send data to salesforce.

Here is my form :

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;
}

Form is redering correctly. following is my validate function :

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

Following is my submit code which is not working : Ideally the cURL call should not return because I have mentioned retURL in form and retrurn URL will be set by salesforce. Currently I am not redirected to retURL.NO any entry is going to 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);

}
  • 写回答

1条回答 默认 最新

  • dongxiusuo9881 2015-05-06 12:24
    关注

    I think you want...

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

    I know this sounds stupid, but you have checked to see if you have cURL installed and it is enabled in php right ?

    Also you should try checking $result for false.

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

    Just to see if something is up, like you cannot post of salesforce because of some security thing, like you not passing in your salesforce.com username and password or the API key for your account.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集