开发者

php cURL POST how to follow location

I am in a bit of a rut with a cURL issue. The post works greate, the data is POSTED just fine and received ok, but the url of the posted page never appears in the browser after the cURL session is executed, for example look at the following code:

    $ch = curl_init("http://localhost/eterniti/cart-step-1.php");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "e开发者_开发知识库rror=1&em=$em&fname=$fname&lname=$lname&email1=$email1&email2=$email2&code=$code&area=$area&number=$num&mobile=$mobile&address1=$address1&address2=$address2&address3=$address3&suburb=$suburb&postcode=$postcode&country=$country");
    curl_exec($ch);
    curl_close($ch);

The post works fine and I am taken to the cart-step-1.php where I can process the posted data, HOWEVER the location in the URL address bar of the browser remains that of the script page, in this case proc_xxxxxx.php

Any ideas how to get the URL address to reflect the page I am actually POSTED to?

Thanks a mill


You are looking for :

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);


You can use curl_getinfo to get the last URL you were redirected to:

$url = curl_getinfo($ch , CURLINFO_EFFECTIVE_URL);

and then redirect the user to that page using header


in that case you'll need to do it in html - so your proc_xxxxxx.php returns an html page that contanis a form that actually sends the post data.

what you're currently doing is retrieving a certain page (with parameters) on your server and returning that as the data for the page that the browser requested. your browser still thinks it called proc_xxxxxx.php but the html data it gets back from the server is that from the cart-step-1.php request, but your browser has no way of knowing this.

setting the url in the browser bar to something arbitrary is impossible as it would be an enormous security risk (eg window.url.display = 'http://www.ebay.com/login') :D


After you have done your curl stuff you can redirect the user with header():

header('Location: http://localhost/eterniti/cart-step-1.php');

More complete:

$url = "http://localhost/eterniti/cart-step-1.php";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "error=1&em=$em&fname=$fname&lname=$lname&email1=$email1&email2=$email2&code=$code&area=$area&number=$num&mobile=$mobile&address1=$address1&address2=$address2&address3=$address3&suburb=$suburb&postcode=$postcode&country=$country");
curl_exec($ch);
curl_close($ch);

header('Location: ' . $url);
exit;

Depending on what this site expects on parameters, this might or might not work. If you redirect the browser to this page, it will just be a GET request from the browser. You cannot make the browser sending POST data.


Only thing that comes to my mind (if you want to make the Browser to send the POST data) is:
Instead of using curl, create a form with all values in hidden fields and show a button that says Continue. Then the user has only to press the button.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜