POST data and redirect user by PHP CURL

2019-08-02 06:40发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-02 06:55

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);
查看更多
劳资没心,怎么记你
3楼-- · 2019-08-02 07:03

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.

查看更多
登录 后发表回答