douao3636 2017-07-26 23:29
浏览 73
已采纳

使用PHP将表单数据发送/发布到URL [关闭]

I have a form that submits via POST and I capture the variables once the form is submitted.

How can I concatenate the form data and then POST it to the url then re-direct to the thank you page?

This is not the exact code, I just can't find any normal answers, and I'm sure there is more than one way to do this. Just trying to figure out the simplest way possible.

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");
}

EDIT:

HERE IS THE ACTUAL ANSWER/WORKING CODE:

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) . "
";

  $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);
  • 写回答

2条回答 默认 最新

  • doq8211 2017-07-26 23:38
    关注

    http_build_query

    (PHP 5, PHP 7) http_build_query — Generate URL-encoded query string

    Example:

    <?php
    $data = array(
        'foo' => 'bar',
        'baz' => 'boom',
        'cow' => 'milk',
        'php' => 'hypertext processor'
    );
    
    echo http_build_query($data) . "
    ";
    echo http_build_query($data, '', '&amp;');
    
    ?>
    

    The above example will output:

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

    The rest depends on your flow logic. To post to another script:

    From this answer:

    Possibly the easiest way to make PHP perform a POST request is to use cURL, either as an extension or simply shelling out to another process. Here's a post sample:

    // 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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)