Advertisement
kudraem

products_stock_updater.php

Jun 10th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.51 KB | None | 0 0
  1. <?php
  2. require_once 'uAPImodule.php';
  3. require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
  4.  
  5. // получаем xls c исходными данными и сохраняем в файл
  6. $data_file_url = "http://b2b.technolight.ru/upload/ostatki_tl.xls";
  7. $data = file_get_contents($data_file_url);
  8. $fh = fopen("tmp/tmp_data.xls", "w");
  9. if(!$fh) exit("Не удалось открыть поток для tmp/tmp_data.xls");
  10. fwrite($fh, $data);
  11. fclose($fh);
  12. // переводим xls в csv формат
  13. $obj_PHPExcel_data = PHPExcel_IOFactory::load("tmp/tmp_data.xls");
  14. $objWriter = PHPExcel_IOFactory::createWriter($obj_PHPExcel_data, 'CSV')
  15.     ->setDelimiter(',')
  16.     ->setEnclosure('"')
  17.     ->setLineEnding("\r\n")
  18.     ->setSheetIndex(0)
  19.     ->save("tmp/tmp_data.csv");
  20. // получаем массив с актуальной информацией
  21. $data_to_update = array();
  22. $fh = fopen("tmp/tmp_data.csv", "r");
  23. if(!$fh) exit("Не удалось открыть поток для tmp/tmp_data.csv");
  24. // пропускаем заголовки
  25. fgetcsv($fh);
  26. while ($string = fgetcsv($fh)) {
  27.     $data_to_update[$string[0]] = array(
  28.                                         "stock" => $string[1]
  29.         );
  30. }
  31. fclose($fh);
  32. // подготавливаем объект для запросов по api
  33. $request = new Request(array(
  34.     'oauth_consumer_key'    => 'products_stock_updater',
  35.     'oauth_consumer_secret' => 'ogSrK8VaP1y4Y1nZIOjgtJstoC.AXi',
  36.     'oauth_token'           => 'nmNajT9l4hv3KzY3FHRlMrz1Zs3DgwqdOS0Ya0.n',
  37.     'oauth_token_secret'    => '73dGbiBdAfv2HpMFfcOB3ysZkeyCXAk3xL76QzUN'
  38. ));
  39. // получаем список всех товаров
  40. $all_goods = array();
  41. $cur_page = 1;
  42. do {
  43.     $response = $request->get('/shop/request', array(
  44.         'page' => 'allgoods',
  45.         'pnum' => $cur_page
  46.     ));
  47.     $response = json_decode($response);
  48.     $num_pages = $response->success->paginator->num_pages;
  49.     foreach ($response->success->goods_list as $product) {
  50.         if(isset($data_to_update[$product->entry_art_no])) {
  51.             $data_to_update[$product->entry_art_no]["id"] = $product->entry_id;
  52.         }
  53.     }
  54.     $cur_page++;
  55. } while ($cur_page <= $num_pages);
  56.  
  57. // обновляем информацию о наличии
  58. foreach ($data_to_update as $art_no => $value) {
  59.     if(isset($value["id"])) {
  60.         $response = $request->get('/shop/editgoods', array(
  61.         'method' => 'cnt-save',
  62.         'id' => $value["id"],
  63.         'stock_q' => $value["stock"]
  64.         ));
  65.         $response = json_decode($response);
  66.         echo "<pre>";
  67.         print_r($response);
  68.         echo "</pre>";
  69.     }
  70.     else continue;
  71. }
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement