checkCards.php
unknown
php
2 years ago
8.9 kB
3
Indexable
<!-- https://suppliers-api.wildberries.ru/card/list --> <?php include "../../../db.php"; session_start(); if(isset($_SESSION["crm_plaza_login"])) { $user_id = $_SESSION["crm_plaza_login"]; } else { header("Location: ../../"); } session_write_close(); $body = (json_decode(file_get_contents('php://input'), true)); // print_r($body); // echo json_encode($body); if (isset($_COOKIE["wildberries_store_selection"])) { $storeId = $_COOKIE["wildberries_store_selection"]; $storeStmt = $db->prepare("SELECT * FROM wildberries_store WHERE id = ?"); $storeStmt->bind_param("i", $storeId); $storeStmt->execute(); $storeResult = $storeStmt->get_result(); $rowStore = $storeResult->fetch_object(); } else { $storeStmt = $db->prepare("SELECT * FROM wildberries_store group by id ASC"); $storeStmt->execute(); $storeResult = $storeStmt->get_result(); $rowStore = $storeResult->fetch_object(); } // echo $rowStore->name; $apiKey = $rowStore->api_key; $store_id = $rowStore->id; $supplyLastStmt = $db->prepare("SELECT * FROM wildberries_supply WHERE store_id = ? order by id DESC "); $supplyLastStmt->bind_param("i", $store_id); if($supplyLastStmt->execute()){ $supplyLastResult = $supplyLastStmt->get_result(); $supplyLastRow = $supplyLastResult->fetch_object(); } else { } if (isset($supplyLastRow->supply_id)) { $supply_id = $supplyLastRow->supply_id; } else { $supply_id = ""; } $cardBody = array( "id"=> 1, "jsonrpc"=> "2.0", "params"=> array( "filter"=> array( "order"=> array( "column"=>"id", "order"=>"asc" ) ), "isArchive"=> false, "query"=> array( "limit"=>1000, "offset"=>3000, "total"=>0 ), "supplierID"=>$rowStore->supplier_id, "withError"=> false ) ); $deal = execRestMain("https://suppliers-api.wildberries.ru/card/list", $cardBody, $apiKey); if (isset($deal["error"])) { http_response_code(500); } else { // echo json_encode($deal); $cards = $deal["result"]["cards"]; for ($i=0; $i < count($cards); $i++) { $category = $cards[$i]["object"]; $parent_category = $cards[$i]["parent"]; $countryProduction = $cards[$i]["countryProduction"]; $supplier_id = $cards[$i]["supplierId"]; $cardId = $cards[$i]["id"]; $addin = $cards[$i]["addin"]; $supplyValue=""; $brand = ""; $name=""; $description=""; for ($j=0; $j < count($addin); $j++) { if ($addin[$j]["type"] == "Бренд") { $brand = $addin[$j]["params"][0]["value"]; } else if($addin[$j]["type"] == "Наименование") { $name = $addin[$j]["params"][0]["value"]; } else if($addin[$j]["type"] == "Описание") { $description = $addin[$j]["params"][0]["value"]; } else if($addin[$j]["type"] == "Комплектация") { $supplyValue = $addin[$j]["params"][0]["value"]; } } $createdAt = $cards[$i]["createdAt"]; $updatedAt = $cards[$i]["updatedAt"]; $existCardStmt = $db->prepare("SELECT * FROM wildberries_cards WHERE cardId = ? group by id DESC"); $existCardStmt->bind_param("s", $cardId); if($existCardStmt->execute()){ $existResult = $existCardStmt->get_result(); $rowExist = $existResult->fetch_object(); if (!isset($rowExist->id)) { $lastId = insertCard($rowStore, $cardId, $supplier_id, $category, $parent_category, $countryProduction, $brand, $name, $description, $supplyValue); $nomeclatures = $cards[$i]["nomenclatures"]; for ($j=0; $j < count($nomeclatures); $j++) { $nomeclature = $nomeclatures[$j]; $nmId = $nomeclature["nmId"]; $chrtId = $nomeclature["variations"][0]["chrtId"]; $barcode = $nomeclature["variations"][0]["barcodes"][0]; $stocks = execRest("https://suppliers-api.wildberries.ru/api/v2/stocks?skip=0&take=10&search=".$barcode, $rowStore->api_key); // echo json_encode($stocks); $currentStock = 0; if (isset($stocks["stocks"][0])) { $currentStock = $stocks["stocks"][0]["stock"]; } for ($k=0; $k < count($nomeclature["variations"][0]["addin"]); $k++) { $variationAddin = $nomeclature["variations"][0]["addin"][$k]; if ($variationAddin["type"] == "Розничная цена") { $price = $variationAddin["params"][0]["count"]; } } $addinNomeclatures = $nomeclature["addin"]; for ($k=0; $k < count($addinNomeclatures); $k++) { if ($addinNomeclatures[$k]["type"] == "Фото") { $photo = $addinNomeclatures[$k]["params"][0]["value"]; } } insertNomeclature($rowStore, $nmId, $chrtId, $lastId, $barcode, $price, $photo, $currentStock ); } } } } } function insertNomeclature($rowStore, $nmId, $chrtId, $cardId, $barcode, $price, $photo, $currentStock) { include "../../../db.php"; $nomeclatureStmt = $db->prepare(" INSERT INTO nomenclatures( nmId, chrtId, card_id, barcode, price, photo, stock) VALUES(?,?,?,?,?,?,?)"); $nomeclatureStmt->bind_param("iiisisi", $nmId, $chrtId, $cardId, $barcode, $price, $photo, $currentStock ); if($nomeclatureStmt->execute()){ echo "updated"; } else { http_response_code(500); die("Error: can't add your offer to database"); } } function insertCard($rowStore, $cardId, $supplier_id, $category, $parent_category, $countryProduction, $brand, $name, $description, $supplyValue) { include "../../../db.php"; $offersStmt = $db->prepare(" INSERT INTO wildberries_cards( cardId, supplier_id, category, parent_category, countryProduction, brand, name, description, supplies, store_id) VALUES(?,?,?,?,?,?,?,?,?,?)"); $offersStmt->bind_param("sssssssssi", $cardId, $supplier_id, $category, $parent_category, $countryProduction, $brand, $name, $description, $supplyValue, $rowStore->id ); if($offersStmt->execute()){ echo "updated"; } else { http_response_code(500); die("Error: can't add your offer to database"); } $last_id = $db->insert_id; return $last_id; } function execRestMain($url, $params, $token) { // echo json_encode($params); $queryUrl = $url; // $queryData = http_build_query($params); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_HEADER => 0, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $queryUrl, CURLOPT_POSTFIELDS => json_encode($params) )); $headers = array( "Authorization: ".$token, "Content-Type: application/json; charset=utf-8", ); // echo json_encode($headers); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $res = curl_exec($curl); curl_close($curl); return json_decode($res, true); } function execRest($url, $token) { $queryUrl = $url; // $queryData = http_build_query($params); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_POST => 0, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $queryUrl, // CURLOPT_POSTFIELDS => json_encode($params) )); $headers = array( "Authorization: ".$token, "Content-Type: application/vnd.api+json", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $res = curl_exec($curl); curl_close($curl); return json_decode($res, true); } ?>
Editor is loading...