【问题标题】:How to convert a POST to a url?如何将 POST 转换为 url?
【发布时间】:2013-07-01 13:00:26
【问题描述】:

是否可以发送数据,您通常必须使用提交按钮来发送它,仅使用 url?

下面是一个带有类似按钮的网站的代码示例:

<form method="POST" action="Action.php?action=338&amp;n=688&amp;t=mine"><input type="hidden" id="Mine688" value="1" name="duree"><button value="submit" class="boutonsGeneral" type="submit" name="travail"><span title="" class="infoBoutonsGeneral"><span class="boutonsGeneral_separateur">&nbsp;</span><span class="boutonsGeneral_gain" id="gainMine688"><img width="48" height="24" src="images/items/itemArgent.png">+2,18</span></span>Munca <span class="boutonsGeneral_duree boutonsGeneral_dureeMoins" id="dureeMine688">1 hour</span></button></form>

当我单击该按钮时,这是来自 Live HTTP 标头:

ww.kraljevine.com/Action.php?action=338&n=688&t=mine

POST /Action.php?action=338&n=688&t=mine HTTP/1.1
Host: ww.kraljevine.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ro-ro,ro;q=0.8,en-us;q=0.6,en-gb;q=0.4,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: ww.kraljevine.com/EcranPrincipal.php?l=8
Cookie: PHPSESSID=ec95ed7caf7c28f8a333; KC=account_name; KC2=1502cae30e04f5f55963e93; > > Glup=274; pageTamponVue=1
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 22
duree=1&travail=submit

我想要做的是通过 url 发送帖子。类似的东西:

http://www.kraljevine.com/Action.php?action=338&n=688&t=mine(直到这里是来自站点的链接,我想添加这个)duree=1&travail=submit 不工作http://www.kraljevine.com/Action.php?action=338&n=688&t=mine&duree=1&travail=submit,因此我想知道是否可以这样做。

【问题讨论】:

  • 您可以使用 cURL 发送后参数。
  • 使用方法 GET man。这要简单得多。使用 get 你有 url 中显示的变量。
  • 无缘无故地把事情复杂化。如果参数进入 QUERYSTRING 使用 GET,如果它们进入表单使用 POST。就这么简单,并且在 php 代码中以相同的方式访问两者。

标签: php post input http-post


【解决方案1】:

首先你需要知道POSTGET是干什么用的。

  • 如果您想发送数据,您应该使用 POST

  • 如果您想根据某些参数 GET 数据,请使用 GET

使用 GET 发送数据是没有意义的,这也有一些限制。您可以阅读有关限制的更多信息here

您始终可以使用 CURLPOST 数据发送到另一个 URL。

Here is good tutorial how to do it 下面是基于本教程的示例:

<?php
$url = 'http://www.kraljevine.com/Action.php?action=338&n=688&t=mine';
$fields = array(
                    'duree' => urlencode('1'),
                    'travail' => urlencode('submit')
            ); 
//$fields can be extended with more params for POST submit always encode them with urlencode for these 2 examples of 1 submit it is not necessary but sometimes is really imporant to do it

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
?>

最后是$_REQUEST,它是一个关联数组,默认包含$_GET$_POST$_COOKIE的内容. 您可以在 php 脚本中使用它,并且为 php 脚本传递变量的方式没有区别。

明智地使用它。

【讨论】:

    【解决方案2】:

    制作表单方法 GET 并将变量放在表单的隐藏字段中。 例如:

    <form method="GET" action="Action.php">
     <input type="hidden" name="action" value="338" />
     <input type="hidden" name="n" value="688" />
     <input type="hidden" name="t" value="mime" />
     ...more html/php...
    </form>
    

    【讨论】:

      【解决方案3】:

      简短的回答是否定的。

      根据定义,GET 变量在 URL 中发送, 和 POST 变量在请求正文中发送。

      因此,您不能按照您的要求仅使用 URL 发送变量。

      即使在 POST 请求中,您也可以通过参数物理捕获可能的 URL,但我强烈建议您不要采用这种方法。

      您可以在this 帖子中了解更多信息

      【讨论】:

        【解决方案4】:

        在我使用弹簧靴的情况下。 我已经像这样从 POST 转换为 GET :

        之前

        在jsp(发件人)中:

        在 Java 中(接收者):

        我已转换为:

        之后

        在 Jsp 中:

        在 Java 中:

        结果与我想要的匹配。我只改变了图片中箭头指向的地方,它节省了我很多时间。所有参数都将轻松组合成对象“searchModel”。

        【讨论】:

          猜你喜欢
          • 2012-04-02
          • 1970-01-01
          • 1970-01-01
          • 2010-12-15
          • 1970-01-01
          • 2013-02-03
          • 1970-01-01
          • 2018-09-17
          • 2021-07-27
          相关资源
          最近更新 更多