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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。