-1

After executing cUrl method, my browser stays on same page, but loads content of page called in a curl init.

Is it possible to redirect browser to that URL?

$ch=curl_init($URL); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$DataToSend);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); 
$varResponse=curl_exec($ch); 
curl_close($ch); 

echo $varResponse;

exit;

UPDATE: So, is not about redirecting, it's about posting data using curl.

Thank you in advance!

user198003
  • 11,029
  • 28
  • 94
  • 152
  • to redirect you don't need curl. just return a 301 redirect https://www.google.co.il/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=http%20redirect%20code – carmel Jan 04 '17 at 17:25
  • it's not about redirecting, it's about posting data using curl... so that's why i need curl – user198003 Jan 04 '17 at 17:26
  • You could redirect there with a `header('Location...')` but whether the target site will show what you intend is unknown. It could depend on a session cookie for example, which may result in a different display for your user's client browser than for what your server POSTed to it. – Michael Berkowski Jan 04 '17 at 17:30
  • @user198003 You asked "Is it possible to redirect browser to that URL". what about posting data you need to know? – carmel Jan 04 '17 at 17:33

2 Answers2

0

This is how to redirect the browser to the URL gathered by cURL:

$ch=curl_init($URL); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$DataToSend);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); 
$varResponse=curl_exec($ch); 
curl_close($ch); 

header('Location: ' . $varResponse);
exit;
Fabian Horlacher
  • 1,899
  • 1
  • 24
  • 31
0

curl is for calling a URL and fetching the response.

If you echo it to the browser then the browser will show the response that was fetched from the URL.

Example

If you have a website. with domain website.com.

And when a user goes to the website.com home page, you do

$ch=curl_init("www.google.com"); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$DataToSend);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); 
$varResponse=curl_exec($ch); 
curl_close($ch); 

echo $varResponse;

exit;

The user will still be on website.com but the content of the page will show the html that google.com returns.

carmel
  • 902
  • 7
  • 24