curl如何发布数据?

时间:2013-03-31 01:45:14

标签: php curl

所以,我有这个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页面上填写另一个表单,其中包含这些已发布的变量,然后将该页面提交到另一个页面。或者是否有某种方法可以放置重定向或者保存最终页面的帖子变量的东西?

2 个答案:

答案 0 :(得分: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'];
?>

答案 1 :(得分: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
}