【问题标题】:POST data and redirect user by PHP CURLPOST 数据并通过 PHP CURL 重定向用户
【发布时间】:2017-09-14 16:38:39
【问题描述】:

我们需要将用户重定向到一个 URL 并使用 PHP CURL 将一些数据发送到该 URL (POST)。就像用户点击 HTML 表单时使用 POST 方法提交一样。

我们的代码是

$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);

但它不起作用。

解决办法是什么?

【问题讨论】:

    标签: php redirect curl post submit


    【解决方案1】:

    这是我使用 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);
    

    如果上述方法不起作用,还有一个替代解决方案:

    $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);
    

    【讨论】:

      【解决方案2】:

      我发现,仅使用 PHP 是不可能的。

      您可以使用 PHP 生成表单,然后在 onLoad 中使用 JS 提交。

      Here 更详细。

      【讨论】:

        猜你喜欢
        • 2019-06-21
        • 2011-03-03
        • 1970-01-01
        • 1970-01-01
        • 2011-02-26
        • 2012-02-22
        • 1970-01-01
        • 2011-11-02
        • 2020-07-21
        相关资源
        最近更新 更多