Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent to be redirected with PHP cURL

Tags:

php

curl

I'm developing a form in which requires to submit the collected data to a third party website, in the form of: http://www.domain.com/page?key=value&key2=value2

I decide to use cURL as I have not found an alternative that convince me.

The problem that I'm running is that once the form is submitted, cURL is executed but I'm being redirected to the domain that I specified. Instead, I want to redirect the user to a confirmation page within my domain and not to the third party website.

Here is an example of the code that I'm using:

$URL="otherserver.domain.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://$URL"); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "key=value2&key2=value2&key3=value3");
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close ($ch);

How can I prevent from being redirected to otherserver.domain.com?

Please feel free to let me know if you think that instead of using cURL, there is a better way to submit the data to the third party website.

Thank you all in advance

like image 472
Ole Media Avatar asked Aug 06 '11 00:08

Ole Media


1 Answers

Try this:

  <?php
$url = "http://***.."; 
$ch = curl_init($url);
$opts = array(CURLOPT_RETURNTRANSFER => 1,
          CURLOP_HEADER => 1,
          CURLOPT_FOLLOWLOCATION => 0,
          CURLOPT_POST => 1,
          CURLOPT_POSTFIELDS => "foo=ba"); 
curl_setopt_array($ch, $opts); 
echo curl_exec($ch);
?>
like image 175
The Mask Avatar answered Oct 11 '22 04:10

The Mask