This code in PHP sends a HTTP POST to a Django app using CURL lib.
I need that this code sends POST but redirect to the page in the same submit. Like a simple form does.
The PHP Code:
$c = curl_init();
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_URL, "http://www.xxx.com");
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, 'Var='.$var);
curl_exec($c);
curl_close ($c);
In this case, the PHP is sending the HTTP POST, but is not redirecting to the page. He is printing the result. My URL still .php and not a django/url/
I need be redirected to the django URL with the Post like a simple form in HTML does.
Any Idea?
Thanks.
Your code does a server-side POST request to the page. You can't "redirect" the user to the same "instance" of the page.
If you need to do it in one step, print out a form with method="POST" and hidden fields and then add JavaScript which automatically submits it.
Related
I try to save data from a FORM to file. But when 'submit' to external URL my script doesn't see $_POST array. How to save $_POST which I send not receive.
I can save $_POST data I received (I sent to my script and save as post_array.txt). But I have to send it to external url.
I tried to receive and resend saved $_POST using cURL but I cannot do redirect with $_POST.
So my customer stays on my page but should be redirected to payment page with $_POST data.
html : <form method="POST" action="cert.php">
php : cert.php
file_put_contents('post_array.txt', $_POST, FILE_APPEND);
$url = 'https://sandbox.przelewy24.pl/trnDirect';
$fields =['p24_merchant_id' => $_POST['p24_merchant_id'],
'p24_session_id' => $_POST['p24_session_id'],
'p24_amount' => $_POST['p24_amount'],
'p24_currency' => $_POST['p24_currency'],
'p24_sign' => md5($_POST['p24_session_id'].'|'.$_POST['p24_merchant_id'].'|'.$_POST['p24_amount'].'|'.$_POST['p24_currency'].'|'.$_POST['p24__sign'])];
//open connection
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
// this cURL dosnt have a redirect option coz it doesnt work for me :(
// execute!
$response = curl_exec($ch);
file_put_contents('response.txt', $response, FILE_APPEND);
// close the connection, release resources used
curl_close($ch);
Because cURL doesnt work as expected i want to direct send $_POST do external page. (it works well , but i dont have saved $_POST in my file)
How to save $_POST without sending data to my server/script ?
You can make a redirect with http status 307 developer.mozilla.org/en-US/docs/Web/HTTP/Status/307 In this case also body will be redirected.
Another option is to do it with js, first make a request to you server, receive successful answer and then make second request using js to external URL with POST method. Maybe you will need some hidden form to do it in browser.
I have created a simple classic ASP script that will take a username and password from a post and create session variables. This script works fine if i use a standard html form and redirect to this page. I have a php site and I want to log users into both websites when they log into the php site. To do this i wanted to add a curl request to the login script in php. This would send the password and username over to the script and create the session variables. The response i get from the curl request would suggest that it worked, but it doesnt seem to be saving the session.
Here is the curl request.
$postinfo = "username=".$username."&password=".$password;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
I dont want to paste the full asp script, but this is roughly how it works. The session persists when i login using a html form so i know its working correctly. When the curl request is finished executing it seems that the session variables are populated, but when i visit another page the session does not exist.
'do some stuff with the db to check if the credentials work.
if success = true then
Session("userid") = userid
Session("login") = "good"
Response.Write("Login successful - " & Session("userid"))
else
Response.Write("Login Failed")
end if
When i run the curl request the response is "Login successful - 123". This means not only is the login working, but its also setting the session value. The problem is that when i try to visit the asp site it does not detect any session data.
I have verified that the all links are pointing to https://www.website.com. Both websites are under the same domain name, just 2 different subdirectories/languages. They are both running on the same server.
I've read a lot of cURL questions on here and none of them seem to have had the same problem I am having.
I have a form set up to submit the following:
if(isset($_POST['submitted_form']) && $_POST['submitted_form'] == "yes"){
$ch = curl_init("https://www.pipelinedeals.com/web_lead");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_REFERER, "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."");
curl_setopt($ch, CURLOPT_POSTFIELDS, "developer_mode=".$_POST['developer_mode']."&thank_you_page=".$_POST['thank_you_page']."&w2lid=".$_POST['w2lid']."&assign_to_id=".$_POST['assign_to_id']."&lead[lead_source]=".$_POST['lead[lead_source]']."&lead[full_name]=".$_POST['lead[full_name]']."&lead[phone]=".$_POST['lead[phone]']."&lead[email]=".$_POST['lead[email]']."");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
}
Before anyone asks why I am using such a convoluted method to post the data, rather than just submitting the form, it is because I want to use the data for some other purposes before passing it on to PipelineDeals.
It's so close to working properly I can taste it. My problem is that when I run the code on say "http://exampledomain.com" it goes ahead and posts the data perfectly. The problem is it isn't performing the redirect properly.
What happens is the data posts. "http://exampledomain.com" is left in the address bar, the content of "https://www.pipelinedeals.com/web_lead" loads on the page (and loads in a way that demonstrates it received the posts), then the content of "http://exampledomain.com" loads under it. It's like cURL is pulling in the content of the other page, rather than redirecting the user to it.
Any suggestions?
It's like cURL is pulling in the content of the other page, rather
than redirecting the user to it.
cURL won't actually redirect the user to the page in question. The client's browser is completely oblivious to what cURL is doing. As far as the browser is concerned, it is simply receiving this data back from your server (which is executing the POST request).
To solve this, you might want to setup a hidden form and then submit that form via JavaScript. Small example:
<form id="pipeline" action="https://www.pipelinedeals.com/web_lead" method="post">
<input type="hidden" name="w2lid" value="<?php echo $_POST['w2lid']; ?>">
<!-- the rest of your POST vars -->
</form>
And the JS:
document.getElementById('pipeline').submit();
Because that is exactly what curl does. It grabs the output of your request, and then you can do some fancy things with it if you like, and then you use that information to render your own page for example.
Are you not looking for: header("Location: http://www.example.com/"); /* Redirect browser */
http://php.net/manual/en/function.header.php
(possibly together with the CURL of course)
What I am trying to do is open a url and pass variables using PHP,
Everytime I use the cURL method it opens the page and displays the output and won't follow the redirects afterwards.
The HttpRequest is not found.
What can I do?
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
For privacy issues I cant post what the url is but thats the code thats giving me the problem when It curls the page it actually displays what it's curling.
I've been banging my head against a wall for a few hours now - and it's probably something really obvious I've missed!
I'm trying to connect to a payment service provider (PSP) using CURL, post data and follow the post so the user actually ends up on the PSP's site.
Using the following:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://psp.com/theirpage');
curl_setopt($ch, CURLOPT_REFERER, "http://mysite.com/mypage");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_POST, 1);
$result=curl_exec($ch);
curl_close($ch);
This successfully connects, verifies the data I've passed, but instead of redirecting the user to the PSP, it just loads the HTML on my site. Safe mode is off, and open_basedir is blank.
What am I doing wrong?
CURL would do an internal redirect and it wont have any effect on the user viewing your curl script. Keep in mind that the payment was made by your server NOT the users computer, hence expecting the session to work for the user is incorrect. cURL 'is the browser'.
If you just want a redirect after payment is made via cURL, you will have to do it via header() or by using some JS like window.location.
The curl request is being made from your server, and as such your server is receiving the response page. There's no way to initiate the request from the server and have the client receive the response. Either return the HTML to the user from your site (as you're doing), or make the request from the client's browser using Javascript. Hope that helps