我正在尝试使用CURL将数据发送到api。我从邮递员生成了php代码,如下所示:
curl_setopt_array($curl, array(
CURLOPT_URL => "https://sandbox.momodeveloper.mtn.com/collection/v1_0/requesttopay",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\r\n \"amount\": \"1012.0\",\r\n \"currency\": \"EUR\",\r\n \"externalId\": \"Yt6sgsjak\",\r\n \"payer\": {\r\n \"partyIdType\": \"MSISDN\",\r\n \"partyId\": \"46733123453\"\r\n },\r\n \"payerMessage\": \"G Tax Renewal\",\r\n \"payeeNote\": \"G Tax Renewal\"\r\n}",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $token",
"X-Reference-Id: $xref",
"X-Target-Environment: sandbox",
"Ocp-Apim-Subscription-Key: $OcpApim",
"Content-Type: application/json"
),
));
但是我有一种用于提交数据的表格,因此我正在考虑将CURLOPT_POSTFIELDS替换为下面的json_encode变量。
$paymentData =json_encode(array(
"amount"=> "1200",
"currency"=> "EUR",
"externalId"=> "wuwyuewhhsja",
"payer" => array(
"partyIdType" => "MSISDN",
"partyId " => "096152417"
),
"payerMessage" => "Please enter PIN",
"payeeNote" => "Some charge",
), JSON_FORCE_OBJECT);
我尝试了CURLOPT_POSTFIELDS => $ paymentData和CURLOPT_POSTFIELDS =>“ $ paymentData”,但是从服务器收到500错误响应。我被困住了,试图在互联网上寻找解决方案,但都无法正常工作。但是,当我回显$ paymentData时,我实际上看到的是格式正确的json结果。