简体   繁体   中英

How to change the address bar location using PHP cURL for a POST request

I am using the following code to send a POST request using cURL. This is working perfectly. The only issue I have is that the location in the address bar does not update itself.

The webpage that sends this request is www.somedomain.com/merge.php, but after the post has been executed the address bar still shows www.somedomain.com/merge.php instead of www.somedomain.com/preview.php

I have tried using curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); with no luck.

$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'www.somedomain.com/preview.php');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, 'landscape=true');
curl_exec ($c);
curl_close ($c); 

Thank you

You are misunderstanding what's going on. There are three parties involved:

client (browser)  <---->   your server  <---->   somedomain.com

The client sends an HTTP request to your server, your server sends an HTTP request to somedomain.com. Your server will receive the response, the client has nothing to do with it. If you want to redirect the client, you need to issue an appropriate HTTP response to the client from your server telling it to redirect elsewhere. Because the client is talking to your server, not somedomain.com. Whatever is going on between your server and somedomain.com is none of its business.

If you want the client to directly send a POST request to somedomain.com, you need to create a form that POSTs to somedomain.com or trigger something equivalent using Javascript.

You cannot use cURL to do this as the REQUEST did not initiate within the browser, rather it came from PHP.

If you want the browser to update the URL then you should look at posting a FORM from within the current page.

When you make a request using curl, you are programmatically accessing the url and this happens only in the server side.

This will not change the url in the address bar.

If you want to redirect to the new url then use

header('redirect', 'http://newurl.com');

This is impossible. You use cURL and make request from server. But you can change URL on client.

You can use

Header("Location:http://www.somedomain.com/preview.php") after curl_close($c);

But I think this is not correct solution.

It is not possible.

This is the same question at PHP Post & Redirect with cURL Same As HTML Form

Although that question was closed, you can read the valid answer to help you clarify the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM