0

所以,我有这个 cURL 代码,我在网上找到了一个模板

<?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;
?>

我的问题是,如何调用要在 cart2.php 页面上使用的已发布变量,我将使用这些已发布变量在 cart2.php 页面上填写另一个表单,然后将该页面提交到另一个页面。或者有什么方法可以放置重定向或保存最后一页的帖子变量的东西?

4

2 回答 2

1

试试这个

   //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);

在你的cart2.php

<?php
if (isset($_POST['ssl_merchant_id'] )
$ssl_merchant_id=$_POST['ssl_merchant_id'];
?>
于 2013-03-31T01:52:07.047 回答
0

在 上cart2.php,您将使用 $ _POST全局变量:

var_dump($_POST['ssl_merchant_id']);

请注意,如果您不想生成通知并且还需要默认值,则应检查其是否存在:

function getPOST($key, $default = NULL) {
    return isset($_POST[$key]) ? $_POST[$key] : $default;
}

$ssl_merchant_id = getPOST('ssl_merchant_id');
if ($ssl_merchant_id) {
    // Do something
}

或者,您可以使用一种不太安全但更方便的方法进行extract

extract($_POST);
if (isset($ssl_merchant_id)) {
    // Do something
}
于 2013-03-31T01:49:41.587 回答