Gareth Dackevych-Thomas Gareth Dackevych-Thomas - 6 months ago 35
PHP Question

POST a form using cURL to a cross-domain url and add form data to local database

After much searching of resources I've not been able to quite achieve what I've been after.

I have a form (index.html) that, on submission, is being added to a database (submit.php). After that, two fields (name and email) are being submitted to a url that is not on the same domain - our clients CRM deals with it.

This works fine, except that the page that is returned at the end of the request is still submit.php and the content is the response from the other url instead of redirecting the header to the location that I have set.

This is what handles the cURLing.

$url = 'https://another.url';

$fields = array(
'name-field' => $name,
'email_address' => $email
);

$post_string = http_build_query($fields);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($ch);

curl_close($ch);

if ($result){
header('Location: success.html');
}


Any ideas on how I can achieve the redirect after the cURL request?

Thanks all for your support.

Answer
$url = "https://another.url?email_address".$email;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);

curl_close($ch);

if ($result){
    header('Location: success.html');
}