Issue
I am having some trouble making a PUT call to an external API using cURL PHP, the response I am getting all the time is {"status":1,"info":"Updated"}Status code: 200
which should mean that the changes have been properly saved onto the API. But everytime I check, there has not been any change at all.
For your better understanding, this is the process I am following:
The API holds documents such as bills and so, at this moment, I am using two different documents. First, I get one of them and extract the information to store it inside an array. Next, I do the same with the second document to finally do an array_push
and annex one array to the other.
My ultimate goal here was to update one of the original documents maintaining its data while adding info from another different document. I can't just create a new document nor delete
the existing one and post
a new one. The only other option the API gives is by using put
.
At first the only answer I got was the one I wrote above. Then, inspecting more carefully the documentation, I saw that there are discrepancies between how some things are called.
This is a sample test document I am using to try things out:
{
"id": "5e4cf5676a97281ab7116036",
"contact": "5db0745c6a972835922bfac9",
"contactName": "Tiempos mejores vendrĂ¡n",
"desc": "TEST",
"date": 1580979341,
"dueDate": null,
"notes": "hola",
"tags": [
"cp"
],
"products": [
{
"name": "hola",
"desc": "IVA",
"price": 200,
"units": 1,
"tax": 10,
"taxes": [
"s_iva_10"
],
"tags": [],
"discount": 0,
"retention": 0,
"weight": 0,
"costPrice": 0,
"sku": 0,
"account": "5cd98e656a9728325261db7a"
}
],
"tax": 20,
"subtotal": 200,
"discount": 0,
"total": 220,
"language": "es",
"status": 0,
And here is the Ajax example found inside the documentation which actually works when done from postman (I am using this as my document 2 data):
"date": 1580979341,
"items": [
{
"name": "hola",
"desc": "IVA",
"subtotal": 200,
"taxes": [
"s_iva_10"
],
"units": 1
}
]
As you might notice, the API returns products
while the example says it's items
. Since that is the only part I really need to update, I started wrapping my array with another one like this:
$receipt = fetchCoSalesReceipt($receiptId, $uri4, $token);
array_push($receipt['products'], $items);
$arrayItems['items'] = $receipt['products'];
Where $items
is the array containing the information of the document 1. I created $arrayItems
to just store and update that part as the API described.
The thing is, now that I've done so, the response I am getting has become: {"status":1,"info":"Updated partially. Sku's not founds: 0, 0, 0, 0, 0, "}Status code: 200 int(1
With this being the json I am passing to the put
function:
{
"items": [
{
"name": "hola",
"desc": "IVA",
"price": 200,
"units": 1,
"tax": 10,
"taxes": [
"s_iva_10"
],
"tags": [],
"discount": 0,
"retention": 0,
"weight": 0,
"costPrice": 0,
"sku": 0,
"account": "5cd98e656a9728325261db7a"
},
[
{
"name": "Llamadas Fijos nacionales",
"subtotal": "374.776444",
"tax": 21
},
{
"name": "Llamadas Moviles nacionales",
"subtotal": "460.440000",
"tax": 21
},
{
"name": "Llamadas Premium 902 Nivel 1",
"subtotal": "87.301236",
"tax": 21
}
]
]
}
As you see, I maintain the original data stored, only to add some new info. Once again, in the documentation there is a dummy code which you can use to try, and inside items, there is this thing called sku
. But leaving it empty or 0 works fine there, I don't really know what could this be, what could I have done wrong or why. I've been trying all day to get past this issue, but to no avail.
This first pic shows the items
structure:
Here is a piece of the API's documentation code where you can see the json has the same format as the one I am using. In fact, all the json fragments I am posting seem to be like the one I am using. But here, the code works and updates as it should:
Lastly. here is the function:
function updateReceipt($receiptId, $uri4, $arrayItems, $token){
$ch = curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $uri4.$receiptId,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => json_encode($arrayItems),
CURLOPT_HTTPHEADER => array("Content-Type: application/json", "key: ".$token)
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$response = json_decode($response, true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Status code: $http_code ";
curl_close($ch);
return $response;
}
As I said, I don't know why sometimes it can update, why others not
Solution
The answer to this question lies completely inside the way the API works, so there is little to explain here.
As I said, I kinda had to merge two documents already existing, but the api returned an array where some of the keys were not the same as the ones you have to use to upload:
API returned array['price']
, but for updating or posting, that same key would become array['subtotal']
. And there were a few more like this one.
In the end, I managed to change the key values from a GET to the posting ones. That was the solution.
Answered By - Berny