Advertisement
yesamarcos

Escrevendo MARCAS FIPE na DB localhost

Apr 29th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2.  
  3. // Conexao com banco de dados
  4. require_once("classes/Database.class.php");
  5. $pdo = Database::conexao();
  6.  
  7. // Tipos de veículos disponíveis na FIPE
  8. $array = ["motos", "carros", "caminhoes"];
  9.  
  10. foreach ($array as $vehicle) {
  11.  
  12.     $ch = curl_init();
  13.     curl_setopt($ch, CURLOPT_URL, "http://fipeapi.appspot.com/api/1/".$vehicle."/marcas.json");
  14.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15.     $output = curl_exec($ch);
  16.     curl_close($ch);
  17.  
  18.     $output = json_decode($output);
  19.  
  20.     foreach ($output as $chave) {
  21.  
  22.         $get = "SELECT fipe_key FROM fipe_marcas WHERE fipe_key = '".$chave->key."'";
  23.         $get = $pdo->query($get);
  24.         $count = $get->rowCount();
  25.  
  26.         if($count == 0){
  27.             $destaque = 0;
  28.             $tipo = $vehicle;
  29.             $create = date("Y-m-d H:i:s");
  30.             $update = date("Y-m-d H:i:s");
  31.  
  32.             $sql = "INSERT INTO fipe_marcas (fipe_id, name, fipe_name, fipe_order, fipe_key, tipo, destaque, created_at, updated_at)
  33.                            VALUES  (:fipe_id, :name, :fipe_name, :fipe_order, :fipe_key, :tipo, :destaque, :created_at, :updated_at)";
  34.  
  35.             $stmt = $pdo->prepare($sql);
  36.  
  37.             $stmt->bindParam(':fipe_id', $chave->id, PDO::PARAM_INT);
  38.             $stmt->bindParam(':name', $chave->name, PDO::PARAM_STR);
  39.             $stmt->bindParam(':fipe_name', $chave->fipe_name, PDO::PARAM_STR);
  40.             $stmt->bindParam(':fipe_order', $chave->order, PDO::PARAM_STR);
  41.             $stmt->bindParam(':fipe_key', $chave->key, PDO::PARAM_STR);
  42.             $stmt->bindParam(':tipo', $tipo, PDO::PARAM_STR);
  43.             $stmt->bindParam(':destaque', $destaque, PDO::PARAM_STR);
  44.             $stmt->bindParam(':created_at', $create, PDO::PARAM_STR);
  45.             $stmt->bindParam(':updated_at', $update, PDO::PARAM_STR);
  46.  
  47.             $stmt->execute();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement