Guest User

Untitled

a guest
Jan 9th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. // ao invés de multiplicar as dimensões pela quantidade, podes passar a quantidade do item conforme abaixo
  10. $products = [
  11.     //produto 1
  12.     [
  13.         'weight' => 1,
  14.         'width' => 10,
  15.         'height' => 10,
  16.         'length' => 10,
  17.         'quantity' => 3, // quantidade desse produto
  18.         'insurance_value' => 50 // valor da unidade do produto
  19.     ],
  20.     //produto 2
  21.     [
  22.         'weight' => 0.5,
  23.         'width' => 15,
  24.         'height' => 15,
  25.         'length' => 15,
  26.         'quantity' => 2,
  27.         'insurance_value' => 50
  28.     ]
  29. ];
  30.  
  31.  
  32. //$products = $this->cart->getProducts();
  33. //$peso = $this->cart->getWeight(); // Peso
  34.  
  35. $me_products = [];
  36. foreach ($products as $product) {
  37.     $me_products[] = array(
  38.             'weight' => $product['weight'],
  39.             'width' => $product['width'],
  40.             'height' => $product['height'],
  41.             'length' => $product['length'],
  42.             'quantity' => $product['quantity'],
  43.             'insurance_value' => $product['insurance_value']
  44.         );
  45. }
  46.  
  47.  
  48. $to_cep = '96055040';
  49. $to_address = 'Rua teste';
  50. $to_number = '123';
  51.  
  52. $body = array(
  53.         'from' => [
  54.             'postal_code' => '01032000',
  55.             'address' => 'Rua brigadeiro tobias',
  56.             'number' => '356',
  57.         ],
  58.         'to' => [
  59.             'postal_code' => $to_cep,
  60.             'address' => $to_address,
  61.             'number' => $to_number,
  62.         ],
  63.         'products' => $me_products,
  64.         'options' => [
  65.             'receipt' => false,
  66.             'own_hand' => false,
  67.             'collect' => false
  68.         ],
  69.         'services' => "1,2,4"
  70.     );
  71.  
  72. echo "<pre>";
  73. print_r($body);
  74.  
  75. $curl = curl_init();
  76. curl_setopt_array($curl, array(
  77.   CURLOPT_URL => "https://melhorenvio.com.br/api/v2/me/shipment/calculate",
  78.   CURLOPT_RETURNTRANSFER => true,
  79.   CURLOPT_ENCODING => "",
  80.   CURLOPT_MAXREDIRS => 10,
  81.   CURLOPT_TIMEOUT => 30,
  82.   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  83.   CURLOPT_CUSTOMREQUEST => "POST",
  84.   CURLOPT_POSTFIELDS => json_encode($body),
  85.   CURLOPT_HTTPHEADER => array(
  86.     "Accept: application/json",
  87.     "Authorization: Bearer TOKEN",
  88.     "Cache-Control: no-cache",
  89.     "Content-Type: application/json",
  90.     "Postman-Token: b4d19c58-1d6b-a653-f225-dc0d719f1d3f"
  91.   ),
  92. ));
  93.  
  94. $response = curl_exec($curl);
  95. $err = curl_error($curl);
  96.  
  97. curl_close($curl);
  98.  
  99. if ($err) {
  100.   echo "cURL Error #:" . $err;
  101. } else {
  102.   //echo $response;
  103. }  
  104.  
  105. //Decodificação pela função json_decode
  106. $objeto_php = json_decode($response);
  107.  
  108. print_r($objeto_php);
Advertisement
Add Comment
Please, Sign In to add comment