Why can't I post to my cURL script?

251 Views Asked by At

I need to send data from one form to two separate locations. I've done this before with a simple cURL script, but now in my testing I can't get it to actually post anything to my secondary source. I am using PAW to send over the POST, with the correct information. Below is my cURL script. I've been looking at this for way too long.

$url = 'https://vf267.infusionsoft.com/app/form/process/41f58953f72a770df094c4525be6fb6b';

$fields = array(
'inf_form_xid' => urlencode($_POST['inf_form_xid']),
'inf_form_name' => urlencode($_POST['inf_form_name']),
'infusionsoft_version' => urlencode($_POST['infusionsoft_version']),
'inf_field_Email' => urlencode($_POST['institution'])
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open 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($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

//execute post
$result = curl_exec($ch);
return $result;
//close connection
curl_close($ch);

I'm passing it these value/key pairs... which correlate to the Infusionsoft form I'm posting to

inf_form_xid=41f58953f72a770df094c4525be6fb6b
inf_form_name=tester
infusionsoft_version=1.50.0.37
[email protected]
2

There are 2 best solutions below

1
On BEST ANSWER
/* manually setting the variables for testing */
$_POST['inf_form_xid']          =   '41f58953f72a770df094c4525be6fb6b';
$_POST['inf_form_name']         =   'tester';
$_POST['infusionsoft_version']  =   '1.50.0.37';
$_POST['institution']           =   '[email protected]';



$url = 'https://vf267.infusionsoft.com/app/form/process/41f58953f72a770df094c4525be6fb6b';
$fields = array(
    'inf_form_xid'          => $_POST['inf_form_xid'],
    'inf_form_name'         => $_POST['inf_form_name'],
    'infusionsoft_version'  => $_POST['infusionsoft_version'],
    'inf_field_Email'       => $_POST['institution']
);


//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);/* set as true rather than a number */
/* use `http_build_query` to construct data to send */
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query( $fields ) );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

/* shows the success page by the looks of things - a graphic and a message saying "Thanks Friend! We will contact you shortly." */
print_r( $result );
0
On

Perhaps the word 'return' ?

Your (modifed) script works for me

<?php

$url = 'https://vf267.infusionsoft.com/app/form/process/41f58953f72a770df094c4525be6fb6b';

$fields = [
    inf_form_xid => '41f58953f72a770df094c4525be6fb6b',
    inf_form_name => 'tester',
    infusionsoft_version => '1.50.0.37',
    inf_field_Email => '[email protected]'
];


//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open 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($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

//execute post
$result = curl_exec($ch);

var_export($result);
die('Finished');

return $result;
//close connection
curl_close($ch);

Output:

<link rel="shortcut icon" href="/slices/style/favicon.ico" type="image/x-icon"/>

<!-- Headers to prevent the caching of pages -->
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-store"/>


<script type="text/javascript" src="/js/lightbox_js.jsp?b=1.50.0.37"></script>
<link rel="stylesheet" href="/css/lightbox_css.jsp?b=1.50.0.37" type="text/css" media="screen"/>
<script type="text/javascript">(function() {
        var styleArray = [];
...
...