【问题标题】:How to send POST data to external url and open the target url如何将 POST 数据发送到外部 url 并打开目标 url
【发布时间】:2013-08-01 12:29:51
【问题描述】:

我想将 POST 数据发送到给定的外部 url,例如 http://www.example.com/catchPostData.php,其中的 catchPostData.php 如下所示:

<?php
echo $_POST['somedata'];
?>

并使用我发送的数据打开此网址。

我尝试使用 cURL,但它从给定的 url 返回,我想留在那里!

谁能帮我解决这个问题?

【问题讨论】:

  • 或者你可以试试ajax post。您需要处理跨域问题。

标签: php url post curl external


【解决方案1】:

我想你正在寻找这样的东西:PHP Redirect with POST data

您将需要第二个页面,将帖子数据提交到完全不同的 url。

【讨论】:

    【解决方案2】:

    您将需要两页:一页用于输入,第二页将输入重定向到外部 url。因此,您的代码将如下所示:

    page1:
    
    <form method="post" action="./page2.php">
    
    <input type="hidden" name="somedata" value="yourData">
    
    <input type="submit" name="submit" />
    
    </form>
    
    
    page2:
    //you can create an array contains your posted data
    //if you are dealing with multiple values
    $array = array("somedata"=>$_POST['somedata']);
    
    //create a url variable that will store all values
    //since you are going to send them as part of a single url 
    $url="";
    
    //if you are posting more than one value you will need to loop
    //through each one - not necessary if you are sending a single value
    
    foreach($array as $key=>$value){
    
        $url .=$key."=".urlencode($value)."&";
    }
    
    header("http://www.example.com/catchPostData.php?$url");
    

    【讨论】:

    • 感谢您的回答,但一切都是这样的: 1. 在第一页单击按钮 -> 按钮执行功能,收集所需数据 -> 数据以无任何形式发送POST -> 外部页面捕获数据并使用它们填充一些字段
    • 没关系...乐于助人
    【解决方案3】:

    你可以使用 document.ready 函数

    $.post({
             url: "you_page.php",
             type: "POST",
             data:{formId:form_id},
             success: function (data){
             }
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      • 2019-01-25
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多