Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // ao invés de multiplicar as dimensões pela quantidade, podes passar a quantidade do item conforme abaixo
- $products = [
- //produto 1
- [
- 'weight' => 1,
- 'width' => 10,
- 'height' => 10,
- 'length' => 10,
- 'quantity' => 3, // quantidade desse produto
- 'insurance_value' => 50 // valor da unidade do produto
- ],
- //produto 2
- [
- 'weight' => 0.5,
- 'width' => 15,
- 'height' => 15,
- 'length' => 15,
- 'quantity' => 2,
- 'insurance_value' => 50
- ]
- ];
- //$products = $this->cart->getProducts();
- //$peso = $this->cart->getWeight(); // Peso
- $me_products = [];
- foreach ($products as $product) {
- $me_products[] = array(
- 'weight' => $product['weight'],
- 'width' => $product['width'],
- 'height' => $product['height'],
- 'length' => $product['length'],
- 'quantity' => $product['quantity'],
- 'insurance_value' => $product['insurance_value']
- );
- }
- $to_cep = '96055040';
- $to_address = 'Rua teste';
- $to_number = '123';
- $body = array(
- 'from' => [
- 'postal_code' => '01032000',
- 'address' => 'Rua brigadeiro tobias',
- 'number' => '356',
- ],
- 'to' => [
- 'postal_code' => $to_cep,
- 'address' => $to_address,
- 'number' => $to_number,
- ],
- 'products' => $me_products,
- 'options' => [
- 'receipt' => false,
- 'own_hand' => false,
- 'collect' => false
- ],
- 'services' => "1,2,4"
- );
- echo "<pre>";
- print_r($body);
- $curl = curl_init();
- curl_setopt_array($curl, array(
- CURLOPT_URL => "https://melhorenvio.com.br/api/v2/me/shipment/calculate",
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 30,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => "POST",
- CURLOPT_POSTFIELDS => json_encode($body),
- CURLOPT_HTTPHEADER => array(
- "Accept: application/json",
- "Authorization: Bearer TOKEN",
- "Cache-Control: no-cache",
- "Content-Type: application/json",
- "Postman-Token: b4d19c58-1d6b-a653-f225-dc0d719f1d3f"
- ),
- ));
- $response = curl_exec($curl);
- $err = curl_error($curl);
- curl_close($curl);
- if ($err) {
- echo "cURL Error #:" . $err;
- } else {
- //echo $response;
- }
- //Decodificação pela função json_decode
- $objeto_php = json_decode($response);
- print_r($objeto_php);
Advertisement
Add Comment
Please, Sign In to add comment