douwen3500 2014-12-03 10:30 采纳率: 0%
浏览 30
已采纳

如何使用curl而不是fopen将数据发送到url

I am integrating Payment gateway. For that one if I use the fopen the data returns correctly. I need to use curl instead of fopen. But I am unable to get the result with curl.

fopen code is like this

$url = "https://test.ctpe.net/frontend/GenerateToken";
$data = "SECURITY.SENDER=xxxxxx" .
   "&TRANSACTION.CHANNEL=xxx" .
   "&TRANSACTION.MODE=xxxx_TEST" .
   "&USER.LOGIN=xxxx" .
   "&USER.PWD=xxxx" .
   "&PAYMENT.TYPE=xxx" .
   "&PRESENTATION.AMOUNT=xxxx" .
   "&PRESENTATION.CURRENCY=xxxx" .
    "&IDENTIFICATION.TRANSACTIONID=xxxxx" ;
$params = array('http' => array(
         'method' => 'POST',
         'content' => $data
       ));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
  throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
  throw new Exception("Problem reading data from $url, $php_errormsg");
}

Here Curl Code

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);       
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
        curl_setopt($ch, CURLOPT_NOBODY, FALSE);
         $response = curl_exec($ch);
        curl_close($ch);

This will returns Format Error data. Please help me. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongxie3963 2014-12-03 10:52
    关注

    You must use $data with CURLOPT_POSTFIELDS option instead of $params.

    Your current code: curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
    It should be: curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测