如何将POST变量发送到外部URL?

时间:2012-05-07 16:43:21

标签: php symfony1 symfony-1.4

我正在制作购物车。我想在进入支付网关之前保存订单。 我的支付网关要求我将POST发送到外部地址,但不是如何通过控制器操作执行此操作。

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

2 个答案:

答案 0 :(得分:1)

使用cURL to post data

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

答案 1 :(得分:0)

在我们的网站(bpremium.com),我们通过ajax运行我们的支付系统,我们的网站通过webservices将“创建销售”或“更新数量”等命令发送到特定网址,并且这些网址记录了购物车的当前状态和将销售ID存储在会话中。

然后当我们到达TPV时,我们执行一个web服务来获取表单的html,生成,签名和散列,准备好按一下按钮。

这种技术非常适合高速运行,因为您不需要继续重定向并强制用户等待,它的重量要轻得多,这意味着您可以将TPV打开到窗口中,填写它并且merchantURL将会捕获成功或失败时来自TPV网关的POST数据。