php - One cURL connection not working, but the second one is

491

I am trying to make a curl connection that sends a post submission to two external db's the first connection gets the raw post array, the second gets a reconstructed associative array. Strangely the first is not working but the second is.

I cannot figure out how to get this editor to let me submit code. I have tried pressing the code button in the menu, I have tried putting 4 spaces in front of all lines, but it keeps complaining about code everytime I submit.

I can provide the form, the curl functions, and a print_r of the post array and the output of the results from curl connections.

I do know that the only difference to both connections really are the local ports that are being used (which I find strange) and the one that works has d/l values where as the the one that doesn't work the d/l values are 0.

In the meantime I will see if I can show you copies of everything.

    $post_data2['name']=$_POST['first_name'];
$post_data2['custom_LastName1']=$_POST['last_name'];
$post_data2['email']=$_POST['email'];
$post_data2['custom_Phone']=$_POST['phone'];
$post_data2['webform_id'] = 8114201;
$post_data1=$_POST;


//create cURL connection for salesforce db
$ch1 =  curl_init('https://test.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8');
print_r($_POST); 
//set options
//set options
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch1, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($ch1, CURLOPT_POSTFIELDS, $_POST);

//perform our request
curl_exec($ch1);


$result = curl_exec($ch1);

//show information regarding the request
print_r(curl_getinfo($ch1));
echo 'errors';
echo curl_errno($ch1) . '-' . curl_error($ch1);

//close the connection
curl_close($ch1);



//curl connection for getresponse db
$ch2 = curl_init('https://app.getresponse.com/add_contact_webform.html?u=8QGL');

//set options
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch2, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($ch2, CURLOPT_POSTFIELDS, $post_data2);

//perform our request
curl_exec($ch2);
$result = curl_exec($ch1);

//show information regarding the request
print_r(curl_getinfo($ch2));
echo curl_errno($ch2) . '-' .curl_error($ch2);


curl_close($ch2);

 output  including a print_r($_POST).  The first curl output is the broken one, the second is the working one.
 Array ( [oid] => 00DF00000007QKR [first_name] => Jennifer [last_name] => Cowles [email] => [email protected] [phone] => 9286711697 [company] => NWU [Campaign_ID] => 701F0000000mimp [submit] => Yes, call me now! ) Array ( [url] => https://test.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8 [content_type] => text/html;charset=UTF-8 [http_code] => 200 [header_size] => 339 [request_size] => 292 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.097856 [namelookup_time] => 2.3E-5 [connect_time] => 2.7E-5 [pretransfer_time] => 9.9E-5 [size_upload] => 909 [size_download] => 0 [speed_download] => 0 [speed_upload] => 9289 [download_content_length] => -1 [upload_content_length] => 909 [starttransfer_time] => 0.020784 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 204.14.234.45 [primary_port] => 443 [local_ip] => 10.0.2.142 [local_port] => 48534 [redirect_url] => ) errors0-Array ( [url] => https://app.getresponse.com/add_contact_webform.html?u=8QGL [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 815 [request_size] => 283 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.692412 [namelookup_time] => 0.009802 [connect_time] => 0.102256 [pretransfer_time] => 0.298121 [size_upload] => 590 [size_download] => 19354 [speed_download] => 27951 [speed_upload] => 852 [download_content_length] => -1 [upload_content_length] => 590 [starttransfer_time] => 0.391054 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 207.8.198.26 [primary_port] => 443 [local_ip] => 10.0.2.142 [local_port] => 37214 [redirect_url] => ) 0

Thanks Jennifer Cowles

51

Answer

-

Solution:

It turns out that salesforce need $_POST need to be encapsulated.

This line: curl_setopt($ch1, CURLOPT_POSTFIELDS, $_POST; was changed to: curl_setopt($ch1, CURLOPT_POSTFIELDS, http_build_query($_POST))

And now all is well in the universe!

People are also looking for solutions to the problem: javascript - PHP create file out of POST, then force user to download it - not working

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.