2014-12-03 60 views
0

我正在集成支付網關。對於那個如果我使用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); 

這將返回格式錯誤數據。請幫幫我。提前致謝。

+0

你正在設置'Content-Type:application/json',但數據是不是_JSON編碼。服務器檢查嗎? – VolkerK 2014-12-03 10:39:39

回答

4

您必須使用帶有CURLOPT_POSTFIELDS選項的$ data而不是$ params。

Your current code: curl_setopt($ch, CURLOPT_POSTFIELDS,$params); 
It should be: curl_setopt($ch, CURLOPT_POSTFIELDS,$data); 
+0

對不起,我試過這樣的.unable得到結果 – Krishna38 2014-12-03 11:22:11

+0

請刪除curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Accept:application/json','Content-Type:application/json'));然後再試一次。 – Eric 2014-12-03 11:44:32

+0

Thanks.Now working fine。 – Krishna38 2014-12-03 11:58:23