【问题标题】:Getting error while sending http request via curl通过 curl 发送 http 请求时出错
【发布时间】:2017-08-13 11:39:23
【问题描述】:

我想通过我通过邮递员发送的 php 发送收费请求并且它有效,但是当我用 php 尝试这个时,我得到了错误响应。

我尝试使用 curl 发送请求并使用函数发送请求。但是,在点击 php 后,我得到了 "invalid request" 的响应。

这里是sn-p的代码:

<?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"
} 

【问题讨论】:

  • 那么,您想从我们这里得到什么?我们甚至不知道您使用什么 API 以及它的文档在哪里。
  • @u_mulder 你不明白什么?
  • 我不明白你的问题。
  • @u_mulder 我需要通过 php 发送一个 http 请求,所以我为此使用了一种方法...正在通过 json 中的一些输入调用 Url...当我运行此 php 时出现错误回复这就是我面临的问题..现在你明白了吗?
  • @tanni 你试过我的 naswer 了吗?

标签: php json curl


【解决方案1】:

您需要更改您的 callAPI 方法。

1) 做完json_encode之后就不需要再做urlencode

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

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 2018-06-21
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2015-03-02
    • 2018-08-30
    相关资源
    最近更新 更多