我正在集成支付網關。對於那個如果我使用fopen數據正確返回。我需要使用捲曲而不是fopen。但我無法得到結果curl。如何使用curl而不是fopen將數據發送到url
的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);
這將返回格式錯誤數據。請幫幫我。提前致謝。
你正在設置'Content-Type:application/json',但數據是不是_JSON編碼。服務器檢查嗎? – VolkerK 2014-12-03 10:39:39