【问题标题】:Send/Post Form Data to Url with PHP [closed]使用 PHP 将表单数据发送/发布到 URL [关闭]
【发布时间】:2017-07-26 23:29:43
【问题描述】:

我有一个通过 POST 提交的表单,并在提交表单后捕获变量。

如何连接表单数据,然后将其发布到 url,然后重定向到感谢页面?

这不是确切的代码,我只是找不到任何正常的答案,而且我确信有不止一种方法可以做到这一点。只是想找出最简单的方法。

if(isset($_POST['submit'])){
    $var1 = $_POST['var1'];
    $var2 = $_POST['var2'];

$url = 'https://api.this.com/foo/bar?token=IHAVETOKEN&foo=$Var1&bar=$var2'

post_request_to($url);

header("Location: thankyou.php");
}

编辑:

这是实际的答案/工作代码:

if(isset($_GET['submit'])){
  $firstName = $_GET['firstname'];
  $lastName = $_GET['lastname'];
  $email = $_GET['email'];
  $password = $_GET['password'];
  $phone = $_GET['phone'];
}

  $data = array(
        'token' => 'sadfhjka;sdfhj;asdfjh;hadfshj',
        'firstName' => $firstName,
        'lastName' => $lastName,
        'email' => $email,
        'password' => $password,
        'phone' => $phone

    );


  $postvars = http_build_query($data) . "\n";

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://api.com/foo/bar?');
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $server_output = curl_exec ($ch);

  curl_close ($ch);

【问题讨论】:

  • 您意识到您发布的内容存在解析错误。另外,变量不会用单引号解析。
  • 这不是确切的代码。我找不到一个通用的方法来做到这一点。
  • 另外,变量区分大小写。您的代码在很多级别上都失败了。
  • 欢迎来到 StackOverflow。请阅读How to Askminimal reproducible example 然后编辑您的问题。
  • 这有点太宽泛了。为什么不把你的伪代码变成真正的代码,如果它不起作用或者可能会回来。我认为 SO 社区在这里很慷慨,对于这么多答案来说,这确实是一个广泛的问题。

标签: php forms api post


【解决方案1】:

http_build_query

(PHP 5, PHP 7) http_build_query — 生成 URL 编码的查询字符串

例子:

<?php
$data = array(
    'foo' => 'bar',
    'baz' => 'boom',
    'cow' => 'milk',
    'php' => 'hypertext processor'
);

echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&amp;');

?>

上面的例子会输出:

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor

其余的取决于您的流程逻辑。发布到另一个脚本:

来自answer

可能让 PHP 执行 POST 请求的最简单方法是使用 cURL,或者作为extension,或者干脆向另一个人发泄 过程。这是一个帖子示例:

// where are we posting to?
$url = 'http://foo.com/script.php';

    // what post fields?
    $fields = array(
       'field1' => $field1,
       'field2' => $field2,
    );

    // build the urlencoded data
    $postvars = http_build_query($fields);

    // 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, $postvars);

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

    // close connection
    curl_close($ch)

【讨论】:

  • 我想这就是我要找的东西
  • @Matthew 打赌你是。您需要将它们粘合在一起才能在您的代码中工作。我的回答中的例子应该给你一个很好的开端。如果您遇到问题,请给我留言,我会尽力提供帮助。如果我的答案确实是您正在寻找的,那么将其标记为您问题的答案将是接下来要做的事情。
  • 非常感谢!这正是我需要去的地方。
【解决方案2】:

file_get_contents连接url并做post方法

if(isset($_POST['submit'])) {
  $var1 = $_POST['var1'];
  $var2 = $_POST['var2'];

  $url = 'https://api.this.com/foo/bar?token=IHAVETOKEN&foo='.$Var1.
  '&bar='.$var2;


  $opts = array('http' =>
    array(
      'method' => 'POST',
      'header' => 'Content-type: application/x-www-form-urlencoded'
    )
  );

  $context = stream_context_create($opts);

  $result = file_get_contents($url, false, $context);

  header("Location: thankyou.php");
}

你也可以使用curl

【讨论】:

    猜你喜欢
    • 2013-07-01
    • 2013-05-16
    • 1970-01-01
    • 2011-09-01
    • 2023-03-23
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多