Why cURL not sending my data to salesforce?

by Akki   Last Updated May 05, 2015 21:03 PM

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

}


Related Questions


Is there a real-time inline field validation module?

Updated August 12, 2015 17:03 PM




Using FormState::setError() in Form::submitForm()

Updated March 24, 2016 08:03 AM