2

We need to redirect users to a URL and send some data to that URL (POST) with PHP CURL. Exactly like when a user click on HTML form submit with POST method.

Our code is

$data=array("Token"=>"test2","RedirectURL"=>"test1");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sep.shaparak.ir/Payment.aspx');  
curl_setopt($ch, CURLOPT_REFERER, 'https://sep.shaparak.ir/Payment.aspx'); 
curl_setopt($ch, CURLOPT_HEADER, false);  
curl_setopt($ch, CURLOPT_POST, 1);  
curl_setopt($ch, CURLOPT_VERBOSE, false);  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);  
curl_setopt($ch, CURLOPT_POSTREDIR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_exec($ch);

But it doesn't work.

What is the solution?

2 Answers 2

3

As I found, it is not possible just with PHP.

You can generate a form with PHP and submit that with JS in onLoad.

Here is more detail.

Sign up to request clarification or add additional context in comments.

Comments

0

This is my workable solution for post with curl:

$postdata = "email=".$username."&pass=".$password; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, sizeof($postdata));
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$result = curl_exec ($ch);
curl_close ($ch);

There is an alternative solution if above doesnt works:

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$result = curl_exec ($ch);
curl_close ($ch);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.