· 5 years ago · Oct 01, 2020, 08:04 PM
1<?php
2
3function inserirDados(){
4
5error_reporting(0);
6
7header("Content-Type: application/json");
8require("/bd/conexao.php");
9
10$data_atual = date('d/M/Y');
11
12
13// USUÁRIO E SENHA DO PORTAL, GERANDO O CLIENT_ID E A SECRET_KEY
14
15$usuario = "";
16$senha = "";
17
18
19// DADOS ENVIADOS VIA BODY
20
21$Campo_corpo = array(
22
23 "grant_type" => "password",
24 "username" => "",
25 "password" => ""
26);
27
28
29// CRIAMOS A REQUISIÇÃO PARA BUSCAR O ACESS_TOKEN
30
31$pegar_token = curl_init('https://api.gs1br.org/oauth/access-token');
32
33curl_setopt($pegar_token, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
34curl_setopt($pegar_token, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
35curl_setopt($pegar_token, CURLOPT_USERPWD, "$usuario:$senha");
36curl_setopt($pegar_token, CURLOPT_SSL_VERIFYPEER, false);
37curl_setopt($pegar_token, CURLOPT_RETURNTRANSFER, true);
38curl_setopt($pegar_token, CURLOPT_POST, true);
39curl_setopt($pegar_token, CURLOPT_POSTFIELDS, json_encode($Campo_corpo));
40
41$retorno_token = curl_exec($pegar_token);
42
43$encode_reposta = json_decode($retorno_token);
44$armazena_token = $encode_reposta->access_token; // AQUI GUARDO O [ TOKEN_ACESS ] PARA USAR NA REQUISIÇÃO 2
45
46
47
48$headers = array();
49
50$headers[] = 'Content-Type: application/json';
51$headers[] = 'client_id: ';
52$headers[] = 'access_token:' . $armazena_token;
53
54$pegar_dadosprodut = curl_init();
55
56
57//ITENS DE RESPONSABILIDADE DO BANCO DE DADOS: CONSULTA, INSERÇÃO E DADOS NECESSÁRIOS PARA O LOOP
58
59
60$sql_consultargtinBD = file_get_contents("bd/buscar_gtin.sql");
61$sql_insertgtinBD = file_get_contents("bd/insere_dados.sql");
62$sql_gtins_n_encontrado = file_get_contents("bd/insere_gtins_n_encontrados.sql");
63
64
65// PREPARANDO A CONSULTA RESPONSÁVEL PELOS GTINS QUE IREMOS ENVIAR PARA A API.
66
67$retorno_consultaGtin = $bd_pdo->prepare($sql_consultargtinBD);
68$retorno_consultaGtin->execute();
69$gtin_bd = $retorno_consultaGtin->fetchAll(PDO::FETCH_OBJ);
70
71
72
73
74foreach($gtin_bd as $gtin_retorno){
75
76curl_setopt($pegar_dadosprodut, CURLOPT_URL, "https://api.gs1br.org/gs1/v0/products/".$gtin_retorno->CODAUXILIAR);
77curl_setopt($pegar_dadosprodut, CURLOPT_CUSTOMREQUEST, 'GET');
78curl_setopt($pegar_dadosprodut, CURLOPT_HTTPHEADER, $headers);
79curl_setopt($pegar_dadosprodut, CURLOPT_SSL_VERIFYPEER, false);
80curl_setopt($pegar_dadosprodut, CURLOPT_RETURNTRANSFER, true);
81
82$retorno_dadosprodut = curl_exec($pegar_dadosprodut);
83$armazena_retorno = json_decode($retorno_dadosprodut);
84
85
86if( $armazena_retorno->status == "Válido" AND count($armazena_retorno->product) == 1 ){
87
88
89 $insere_dados = $bd_pdo->prepare($sql_insertgtinBD);
90
91 $insere_dados->bindParam(":descricao", utf8_decode($armazena_retorno->product->tradeItemDescriptionInformation->tradeItemDescription));
92 $insere_dados->bindParam(":codauxiliar", $armazena_retorno->product->gs1TradeItemIdentificationKey->gtin);
93 $insere_dados->bindParam(":gtincodauxiliar", $armazena_retorno->product->gs1TradeItemIdentificationKey->gs1TradeItemIdentificationKeyCode);
94 $insere_dados->bindParam(":pesobruto", $armazena_retorno->product->tradeItemWeight->grossWeight->value);
95 $insere_dados->bindParam(":pesoliq", $armazena_retorno->product->tradeItemWeight->netWeight->value);
96 $insere_dados->bindParam(":nbm", $armazena_retorno->product->tradeItemClassification->additionalTradeItemClassifications[0]->additionalTradeItemClassificationCodeValue);
97 $insere_dados->bindParam(":dirfotoprod", $armazena_retorno->product->referencedFileInformations[0]->uniformResourceIdentifier);
98 $insere_dados->bindParam(":codauxiliarc", $armazena_retorno->product->gs1TradeItemIdentificationKey->gtin);
99 $insere_dados->bindParam(":unidade", $armazena_retorno->product->tradeItemWeight->grossWeight->measurementUnitCode);
100 $insere_dados->bindParam(":dataint", $data_atual);
101 $insere_dados->bindParam(":gpc", $armazena_retorno->product->tradeItemClassification->gpcCategoryCode);
102 $insere_dados->bindParam(":gpcdesc", utf8_decode($armazena_retorno->product->tradeItemClassification->gpcCategoryName));
103 $insere_dados->bindParam(":statusgtin", $armazena_retorno->product->gtinStatusCode);
104 $insere_dados->bindParam(":codcest", $armazena_retorno->product->tradeItemClassification->additionalTradeItemClassifications[1]->additionalTradeItemClassificationCodeValue);
105 $insere_dados->bindParam(":marca", utf8_decode($armazena_retorno->product->brandNameInformation->brandName));
106
107
108 if(!$insere_dados->execute()){ // PEGAMOS OS DADOS DE ERRO CASO EXISTA E MOSTRAMOS NA TELA
109
110
111
112 print_r($insere_dados->errorInfo());
113
114
115 }else{
116
117
118 echo "GRAVADO COM SUCESSO";
119
120
121 }
122
123
124
125 }else { // RESPONSÁVEL POR CONDICIONAR O IF DO STATUS DO RETORNO DO JSON
126
127 $http_code_resposta = curl_getinfo($pegar_dadosprodut,CURLINFO_HTTP_CODE);
128
129 if( $http_code_resposta !== 429 || $http_code_respost !== 502 ){
130
131 $insere_dados = $bd_pdo->prepare($sql_gtins_n_encontrado);
132 $descricao ="PRODUTO NÃO ENCONTRADO";
133 $insere_dados->bindParam(":descricao", utf8_decode($descricao));
134 $insere_dados->bindParam(":codauxiliarc", $gtin_retorno->CODAUXILIAR);
135
136
137 if(!$insere_dados->execute()){ // PEGAMOS OS DADOS DE ERRO CASO EXISTA E MOSTRAMOS NA TELA
138
139
140
141 print_r($insere_dados->errorInfo());
142
143
144 }else{
145
146
147
148 echo "SEM DADOS, GRAVANDO PARA REGISTRO";
149
150
151
152 }
153
154 } else{
155
156 echo "\rLIMITE ATINGIDO. NAO E POSSIVEL MAIS CONSULTAR NA API\r\nEncerrando o script\r\n";
157 exit();
158
159 } // IF DE RESPOSTA HTTP
160
161
162 } // responsável por condicionar o IF do status do retorno do JSON
163
164
165} // CHAVE DO FOREACH
166
167}inserirDados();