1

I know this topic has been widely discussed but in this instance I cannot get cURL to submit my simple form and I cannot see why, although I am sure it is obvious. I have tried many different cURL POST data examples found on StackOverflow but none seem to work.

My current idea is that my form action in this case is the problem because it is not, as far as I understand, the real action which posts the data. I have tried various tools to intercept the headers but all I see is the form action I currently have.

I am trying to use cURL to submit the contact form here, http://www.alpinewebdesign.co.uk/

Here is my latest attempt (in which I have included all the hidden fields and tried unsuccessfully I think to set a different header type).

//create array of data to be posted
$post_data['mact'] = 'FormBuilder,m62b34,default,1';
$post_data['m62b34returnid'] = '15';
$post_data['page'] = '15';
$post_data['m62b34fbrp_callcount'] = '1';
$post_data['m62b34form_id'] = '4';
$post_data['m62b34fbrp_continue'] = '2';
$post_data['m62b34fbrp_done'] = '1';
$post_data['name'] = 'Name';
$post_data['email'] = '[email protected]';
$post_data['message'] = 'Message';
$post_data['m62b34fbrp_submit'] = 'Send';


//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = 
  curl_init('http://www.alpinewebdesign.co.uk');

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

$headers = array();
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$headers[] = 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryM4YWvE6kIZAIC8vY';

curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $headers);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

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

//close the connection
curl_close($curl_connection);

Can anyone see the problem?

Thanks

2
  • Have you checked your error logs? Oct 3, 2016 at 12:40
  • 1
    Side note: $post_string = http_build_query($post_items); is all you need instead of foreach loop and implode
    – andrew
    Oct 3, 2016 at 13:03

2 Answers 2

2

Add

curl_setopt($curl_connection, CURLOPT_POST, true);
1

Maybe this can help you:

Firstly check if cURL library is enabled on your server:

<?=phpinfo();?>

Then try to prepare CURLOPT options as Array (also prepare the "form-data" query)

function prepareCurlOptions($url, $data, $headers) 
{
    $boundary = uniqid();

    $post_fields = "-----" . $boundary . "\r\n";

    $separate = count($data);
    foreach($data as $k=>$v) 
    {
        $post_fields .= "Content-Disposition: form-data; name=\"$k\"\r\n\r\n$v\r\n-----" . $boundary;

        // add \r\n separator after each field, except last one
        if( --$separate > 0 ) 
        {
            $post_fields .= "\r\n";
        }
    }
    $post_fields .= "--";

    return array(
        CURLOPT_URL => $url,
        CURLOPT_POSTFIELDS => $post_fields,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
    );
}

And then set all additional options by curl_setopt_array, for example:

$url = 'http://www.alpinewebdesign.co.uk';

$post_data = array();
$post_data['name'] = 'Name';
$post_data['email'] = '[email protected]';
$post_data['message'] = 'Message';
// ...

$headers = array();
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
// ...

curl_setopt_array($curl_connection , prepareCurlOptions($url, $post_data, $headers));

Renember to make sure that Boundary for headers are the same as for the $post_data fields. For example:

$headers[] = "content-type: multipart/form-data; boundary=---".$boundary"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.