Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

API JSON output Format

I have this code

<?php
$id = $_POST['id'];
$nume = $_POST['nume'];
$rrp = $_POST['rrp'];
$pret = $_POST['pret'];
$stoc = $_POST['stoc'];
$url = $_POST['url'];
$url1 = $_POST['url1'];
$url2 = $_POST['url2'];
$url3 = $_POST['url3'];

$username = "xx@xx.eu";
$password = "aaa";

$apiUrl = "https://partners.services.aaaa.eu/v1/mkpApi/product/save";

$auth    = base64_encode($username . ":" . $password);
$headers = [
     "Authorization: Basic " . $auth,
     "Content-Type: application/x-www-form-urlencoded",
];

$payload[] = [
     "id"              => $id,
     "locale"          => "RO",
     "hidden"          => 0,
     "currency"        => "RON",
     "brand"        => "Decorepublic",
     "name" => $nume,
     "category_id"  => 10008,
     "status"        => "1",
     "vat" => "0", 
     "stock"           => [[
         "warehouse_id" => 1,
         "value"        => $stoc,
     ]],
     "sale_price"      => $pret,
     "rrp" => $rrp,
     "description"     => $nume,
     
     "images" => [
         ["url"   => $url],
         ["url"   => $url1],
         ["url"   => $url2],
         ["url"   => $url3],
     ],
];


$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(["data" => json_encode($payload)]));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

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

print_r($response);

//echo '<br><br>';

echo json_encode($payload);

print $stoc;
?>

At output i have

status":"1","vat":"0","stock":[{"warehouse_id":1,"value":"200"}]

I need to have value without "" like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

status":"1","vat":"0","stock":[{"warehouse_id":1,"value":200}],"

I trie to use print $stoc, but it returns value 1.

I tried get function but it doesn’t work. Any idea?

>Solution :

If you’ve got a string in $stoc and you need to convert it to an integer before you encode it as JSON, then you can use intval.

"value" => intval($stoc)

Demo: http://sandbox.onlinephpfunctions.com/code/8b1ed16c28bfbc47f501981f2bf03f7b1bb9ce45

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading