通过curl发送http请求时出错

时间:2017-08-13 11:39:23

标签: php json curl

我想通过php发送充电请求,我已经通过postman发送了它,但是当我尝试使用php时,我得到了错误响应。

我尝试使用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"
} 

1 个答案:

答案 0 :(得分:0)

您需要更改 callAPI 方法。

1)完成urlencode

后,您不需要json_encode

2)删除字符串中'JsonData='.的不必要的连接。

改变你的方法如下

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;
}