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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛