douchi0028 2013-03-31 01:45
浏览 25
已采纳

curl如何发布数据?

so, I have this cURl code that I found a template for online

<?php

//create array of data to be posted
$post_data['ssl_merchant_id'] = 'xxx';
$post_data['ssl_user_id'] = 'xxx';
$post_data['ssl_pin'] = 'xxx';
$post_data['ssl_transaction_type'] = 'xxx';
$post_data['confirm_code'] = $_POST['confirm_code'];
$post_data['ssl_show_form'] = 'xxx';
$post_data['ssl_cardholder_ip'] = $_POST['ssl_cardholder_ip'];
$post_data['ssl_amount'] = $_POST['ssl_amount'];

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = 
  curl_init('xxx/cart2.php');

//set options

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . 
                curl_error($curl_connection);

//close the connection
curl_close($curl_connection);
echo $result;
?>

My question is, how do I call the posted variables to be used on the cart2.php page I will be filling in another form on cart2.php page with these posted variables then submitting that page to another page. or is there some way I can place a redirect or something that will hold the post variables for the final page?

  • 写回答

2条回答 默认 最新

  • douquan9826 2013-03-31 01:52
    关注

    Try this

       //set POST variables
        $url = 'http://yourdomain.com/';
        $fields = array(
                            'ssl_merchant_id' => urlencode('xxxx'),
                                'field2' => urlencode(value2),
                                'field3' => urlencode(value3),
                                //field goes here 
                    );
    
    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    
    //open connection
    $ch = curl_init('xxx/cart2.php');
    
    //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);
    

    And in your cart2.php

    <?php
    if (isset($_POST['ssl_merchant_id'] )
    $ssl_merchant_id=$_POST['ssl_merchant_id'];
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动