2017-08-13 189 views
-2

我想通過PHP發送通過郵遞員發送的計費請求,但是當我嘗試使用php時,我得到了錯誤響應。通過curl發送http請求時發生錯誤

我試着用curl發送請求並使用函數發送請求。但是,在點擊php後,我得到了「無效請求」的響應。

這裏是代碼片段:

<?php 


define('TML_CHARGE_URL2', 'http://sandbox-apigw.mytelenor.com.mm/v1/mm/en/customers/products/vas'); 


$client_id="MDq0MdGtZUGZWfanE8k2fva7GsLvwS0I"; 
$client_secret="GEzAxTE6YYSfLEAD"; 
$accessToken="ytSxhvjSUfNEurD5M6SOJPm6XAfu"; 

/* CP & Product Codes */ 

$cpid="15"; 
$login="apigwtest"; 
$password="apigwtestpwd"; 
$client_id="175612092873562378"; 
$msisdn="9791000601"; 

$prod_code = "APIGW_TEST"; 



$requestParamList = array("cpID" => $cpid, 
"clientTransactionId" => $client_id, 
"loginName" => $login, 
"password" => $password, 
"id" => array (
    "type" => "MSISDN", 
    "value" => $msisdn 
), 
"productCode" => $prod_code 

); 



function callAPI($apiURL, $requestParamList) { 
$jsonResponse = ""; 
$responseParamList = array(); 
$JsonData =json_encode($requestParamList); 
$postData = 'JsonData='.urlencode($JsonData); 
$ch = curl_init($apiURL); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
echo $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json', 
'Content-Length: ' . strlen($postData), 
    'Authorization: Bearer ytSxhvjSUfNEurD5M6SOJPm6XAfu' 
    ) 
); 
echo $jsonResponse = curl_exec($ch); 
$responseParamList = json_decode($jsonResponse,true); 
return $responseParamList; 
} 


function oneshotpayment($requestParamList) { 
return callAPI(TML_CHARGE_URL, $requestParamList); 
} 
function subscription_payment($requestParamList) { 
    return callAPI(TML_CHARGE_URL2, $requestParamList); 
} 

echo subscription_payment($requestParamList); 
?> 

錯誤響應是象下面這樣:

{ 
    "transactionId": "", 
    "timestamp": "2017-08-13T17:28:24+06:30", 
    "recipientMsisdn": "", 
    "code": "500.023.003", 
    "error": "Internal Server Error", 
    "message": "Request input is malformed or invalid" 
} 
+2

所以,你想要我們做什麼?我們甚至不知道您使用的API以及文檔的位置。 –

+0

@u_mulder你不明白什麼? – tanni

+2

我不明白你的問題是什麼。 –

回答

0

你需要改變你的callAPI方法。

1)你不需要做urlencode你做json_encode

2後),字符串刪除的'JsonData='. unnecessory concatination。

改變你的方法如下面

function callAPI($apiURL, $requestParamList) { 
$postData = ""; 
$responseParamList = array(); 
$postData =json_encode($requestParamList); 
$ch = curl_init($apiURL); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");              
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
echo $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json', 
'Content-Length: ' . strlen($postData), 
    'Authorization: Bearer ytSxhvjSUfNEurD5M6SOJPm6XAfu' 
    ) 
); 
echo $jsonResponse = curl_exec($ch); 
$responseParamList = json_decode($jsonResponse,true); 
return $responseParamList; 
}