如何将 POST 变量发送到外部 URL? [英] How to send POST variables to External URL ?

查看:30
本文介绍了如何将 POST 变量发送到外部 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作购物车.我想在去支付网关之前保存订单.我的支付网关要求我将 POST 发送到外部地址,但不要求我从控制器操作中执行此操作.

I'm making a shopping cart. I would like to save the order before going to the payment gateway. My payment gateway requires me to send a POST to external address but not how to do it from a controller action.

public function executeBuy(sfWebRequest $request)
{
  sfProjectConfiguration::getActive()->loadHelpers('Url');

  // save the order
  $this->order = new Order();
  $this->save
  //etc....

  //go to TPV Payment gateway
  $dsAmount       = (float)$order->getPriceWithShipping() * 100;
  $dsOrder        = (int)$order->getId() * 400;
  $dsMerchantCode = (int)sfConfig::get('app_tpv_merchant_code');
  $dsCurrency     = (int)sfConfig::get('app_tpv_merchant_currency');
  $dsMerchantURL  = url_for('cart/ipn', true, array(
    'sf_culture' => $this->getUser()->getCulture(),
  ));
  $options = array(
    'Ds_Merchant_Amount'            => $dsAmount,
    'Ds_Merchant_Currency'          => $dsCurrency,
    'Ds_Merchant_Order'             => $dsOrder,
    'Ds_Merchant_Titular'           => $order->getAddress()->getCustomer()->getNameAndLastName(),
    'Ds_Merchant_MerchantCode'      => $dsMerchantCode,
    'Ds_Merchant_MerchantURL'       => $dsMerchantURL,
    'Ds_Merchant_MerchantSignature' => $digest,
    'Ds_Merchant_Terminal'          => $dsCurrency
  );

  //how to send post $options variables to external url?
}

推荐答案

使用 cURL 发帖数据:

//set POST variables
$dsMerchantURL = url_for('cart/ipn', true, array(
  'sf_culture' => $this->getUser()->getCulture(),
));

$options = array(
  'Ds_Merchant_Amount' => urlencode($dsAmount),
  'Ds_Merchant_Currency' => urlencode($dsCurrency),
  'Ds_Merchant_Order' => urlencode($dsOrder),
  'Ds_Merchant_Titular' => urlencode($order->getAddress()->getCustomer()->getNameAndLastName()),
  'Ds_Merchant_MerchantCode' => urlencode($dsMerchantCode),
  'Ds_Merchant_MerchantURL' => urlencode($dsMerchantURL),
  'Ds_Merchant_MerchantSignature' => urlencode($digest),
  'Ds_Merchant_Terminal' => urlencode($dsCurrency)
);

//url-ify the data for the POST
foreach($options as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'& ');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $dsMerchantURL);
curl_setopt($ch,CURLOPT_POST, count($options));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

这篇关于如何将 POST 变量发送到外部 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆