how to read the return variables using json Curl and php

Asked

Viewed 1,256 times

4

My problem refers to integration with Cielo and I’ve reviewed everything, but without success.

I read this post: /questions/54039/na-api-da-cielo-em-php

"I understood" what I said in the post and I did so:

<?
//string json contendo os dados de um funcionário
$request = '{
    "MerchantOrder":"2014111703",
    "Customer":{
    "Name":"Comprador crédito simples"
},
"Payment":{
    "Type":"CreditCard",
    "Amount":15700,
    "Installments":1,
    "Descriptor":"123456789ABCD",
    "CreditCard":{
        "Holder":"Teste Holder",
        "ExpirationDate":"12/2030",
        "SecurityCode":"123",
        "Brand":"Visa"
    }
 }
}';

$data_string = json_encode($request, false);

$ch = curl_init("https://apisandbox.cieloecommerce.cielo.com.br/1/sales");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELD, $data_string);
curl_setopt($ch, CURLOPT_HTPPHEADER, array(
    'Content-Type: application/json',
    'MerchantId: ' . $MerchantID,
    'MerchantKey: ' . $MerchantKey,
    'Content-Length: ' . strlen($datastring))
);

$result = curl_exec($ch);
$result = json_decode($result, false);

echo "$result->ProofOfSale";
echo "$result->Tid";
echo "$result->Authorization";
echo "$result->SoftDescriptor";
echo "$result->ECI";
echo "$result->Status";
echo "$result->ReturnCode";
echo "$result->ReturnMessage";
?>

The final part refers to the return variables that Cielo informs comfort this manual:

But nothing comes back.

I really don’t understand anything about json and how it works in php.

If you could help me, I’d be most grateful.

2 answers

1

See this response. The problem is in json_decode($result, true), after the CURL, you have two options:


  1. Change to json_decode($result) and keep the rest as is; or
  2. Keep the json_decode($result, true) and change the $result->ProofOfSale for $result['ProofOfSale'], as well as others, for example.

Also make a var_dump($result) to know how the data is and if they are really "coming".

0

Try sending your json in the format of array. Are you using the json_encode, its function is to transform an array into json, so try using the json_decode:

<?php
//string json contendo os dados de um funcionário
$request = '{
    "MerchantOrderId":"2014111703",
    "Customer":{
    "Name":"Comprador crédito simples"
},
"Payment":{
    "Type":"CreditCard",
    "Amount":15700,
    "Installments":1,
    "SoftDescriptor":"123456789ABCD",
    "CreditCard":{
        "CardNumber":"1234123412341231",
        "Holder":"Teste Holder",
        "ExpirationDate":"12/2030",
        "SecurityCode":"123",
        "Brand":"Visa"
    }
 }
}';

$data_string = json_decode($request, true);

$MerchantID="meuid";
$MerchantKey="meukey";

$ch = curl_init("https://apisandbox.cieloecommerce.cielo.com.br/1/sales");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'MerchantId: ' . $MerchantID,
    'MerchantKey: ' . $MerchantKey,
    'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);
$result = json_decode($result, true);

echo "$result->ProofOfSale";
echo "$result->Tid";
echo "$result->AuthorizationCode";
echo "$result->SoftDescriptor";
echo "$result->PaymentId";
echo "$result->ECI";
echo "$result->Status";
echo "$result->ReturnCode";
echo "$result->ReturnMessage";
?>

Check that line too $result = json_decode($result, true);, because if Cielo is sending you an array, you have to use the function json_encode because below that line you are displaying the return as an object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.