doubo1871 2012-05-07 16:43
浏览 66
已采纳

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

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?
}
  • 写回答

2条回答 默认 最新

  • duaiwo9093 2012-05-07 16:51
    关注

    Use 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条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答