我正在整合支付网关。对于那个,如果我使用 fopen ,数据会正确返回。我需要使用curl而不是 fopen 。但我无法通过 curl 获得结果。
fopen代码就像这样
$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");
}
此处卷曲代码
$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);
这将返回格式错误数据。请帮我。提前谢谢。
答案 0 :(得分:1)
您必须使用CURLOPT_POSTFIELDS选项的$ data而不是$ params。
Your current code: curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
It should be: curl_setopt($ch, CURLOPT_POSTFIELDS,$data);