ProductCollectController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Attribute;
  4. use App\Models\AttributeTranslation;
  5. use App\Models\AttributeValue;
  6. use App\Models\Brand;
  7. use App\Models\BrandTranslation;
  8. use App\Models\Category;
  9. use App\Models\CategoryTranslation;
  10. use App\Models\Product;
  11. use App\Models\ProductStock;
  12. use App\Models\Review;
  13. use App\Models\Upload;
  14. use App\Models\User;
  15. use App\Services\ProductFlashDealService;
  16. use App\Services\ProductService;
  17. use App\Services\ProductStockService;
  18. use App\Services\ProductTaxService;
  19. use App\Utility\ProductUtility;
  20. use Auth;
  21. use Combinations;
  22. use DB;
  23. use Illuminate\Http\Request;
  24. use Illuminate\Support\Facades\Storage;
  25. use Illuminate\Support\Str;
  26. use function count;
  27. use function curl_close;
  28. use function curl_exec;
  29. use function curl_init;
  30. use function curl_setopt;
  31. use function dd;
  32. use function explode;
  33. use function json_decode;
  34. use function json_encode;
  35. use function print_r;
  36. use function rand;
  37. use function set_time_limit;
  38. use function single_price;
  39. use function str_replace;
  40. use function var_dump;
  41. use function var_export;
  42. use const CURLOPT_HTTPHEADER;
  43. use const CURLOPT_RETURNTRANSFER;
  44. use const CURLOPT_SSL_VERIFYHOST;
  45. use const CURLOPT_SSL_VERIFYPEER;
  46. use const CURLOPT_URL;
  47. class ProductCollectController extends Controller
  48. {
  49. protected $productService;
  50. protected $productTaxService;
  51. protected $productFlashDealService;
  52. protected $productStockService;
  53. protected $key = '';
  54. protected $secret = '';
  55. protected $r
  56. = [
  57. 'co.id' => 0.01,
  58. 'com.my' => 1,
  59. 'com.ph' => 1,
  60. 'sg' => 1,
  61. 'co.th' => 1,
  62. 'vn' => 1,
  63. ];
  64. public function __construct(
  65. ProductService $productService,
  66. ProductTaxService $productTaxService,
  67. ProductFlashDealService $productFlashDealService,
  68. ProductStockService $productStockService
  69. ) {
  70. $this->key = get_setting('wanbang_key');
  71. $this->secret = get_setting('wanbang_secret');
  72. $this->productService = $productService;
  73. $this->productTaxService = $productTaxService;
  74. $this->productFlashDealService = $productFlashDealService;
  75. $this->productStockService = $productStockService;
  76. }
  77. public function index() {
  78. $products = [];
  79. $categorys = json_encode(collect(Category::where('id', '>', 0)->get()));
  80. return view('backend.product_collect.index', compact('products', 'categorys'));
  81. }
  82. public function ids() {
  83. $data = json_decode(file_get_contents(dirname(__FILE__) . '/apicj_input2.txt'), true);
  84. $params = [];
  85. $list = $data['data'];
  86. $type = "lazada";
  87. foreach ( $list as $item )
  88. {
  89. $url = parse_url($item);
  90. $id = str_replace('.html', '', str_replace('/products/', '', $url['path']));
  91. $array = explode('-', $id);
  92. $host = explode('.', $url['host']);
  93. $nation = $host[count($host) - 2] . '.' . $host[count($host) - 1];
  94. $newId = str_replace('i', '', $array[count($array) - 1]);
  95. if ( $url['host'] == 'www.alibaba.com' )
  96. {
  97. $type = "alibaba";
  98. $newId = explode('_', $newId)[count(explode('_', $newId)) - 1];
  99. }
  100. $params[] = [
  101. 'id' => $newId,
  102. 'nation' => $nation,
  103. 'url' => $item,
  104. 'name' => '',
  105. 'pic_url' => '',
  106. 'source' => '',
  107. 'rating' => '',
  108. 'price' => '',
  109. 'stock' => '',
  110. ];
  111. }
  112. $categorys = Category::where('id', '>', 0)->get();
  113. return response()->json(compact('params', 'categorys', 'type'), 200);
  114. }
  115. public function data( Request $request ) {
  116. set_time_limit(0);
  117. $page = (int)$request->page;
  118. $limit = (int)$request->limit;
  119. $type = $request->type ?? 'lazada';
  120. $data = json_decode(file_get_contents(dirname(__FILE__) . '/apicj_input2.txt'), true);
  121. $params = [];
  122. $list = $data['data'];
  123. $exits = Product::where('digital', 0)->where('in_storehouse', 1)->where('source', $type)->where('added_by', 'admin')->get();
  124. $has = array_column(collect($exits)->toArray(), 'barcode');
  125. // print_r($has);dd();
  126. foreach ( $list as $item )
  127. {
  128. $url = parse_url($item);
  129. $id = str_replace('.html', '', str_replace('/products/', '', $url['path']));
  130. $array = explode('-', $id);
  131. $host = explode('.', $url['host']);
  132. $nation = $host[count($host) - 2] . '.' . $host[count($host) - 1];
  133. $newId = str_replace('i', '', $array[count($array) - 1]);
  134. if ( $type == 'alibaba' ) $newId = explode('_', $newId)[count(explode('_', $newId)) - 1];
  135. if ( !in_array($id, $has) )
  136. {
  137. $na = str_replace('lazada.', '', $nation);
  138. if ( $type == 'alibaba' ) $na = 'alibaba';
  139. $params[] = [
  140. 'id' => $newId,
  141. 'nation' => $na,
  142. 'url' => $item,
  143. ];
  144. }
  145. }
  146. $products = [];
  147. $count = count($params);
  148. $pages = ceil($count / $limit);
  149. if ( $page < 1 ) $page = 1;
  150. foreach ( $params as $key => $param )
  151. {
  152. $key = $key + 1;
  153. if ( $page == 1 )
  154. {
  155. if ( $key <= $page * $limit )
  156. {
  157. $res = $this->curlGet($param['id'], $param['nation'], $type=='alibaba'?'alibaba':'lazada');
  158. if ( $res )
  159. {
  160. if ( $res['error_code'] == '0000' )
  161. {
  162. $item = $res['item'];
  163. $products[] = [
  164. 'id' => $item['num_iid'],
  165. 'name' => $item['title'],
  166. 'pic_url' => $item['pic_url'],
  167. 'source' => $res['api_type'],
  168. 'rating' => 5,
  169. 'price' => $type=='alibaba'?$item['price']:single_price($item['price'] * ( isset($this->r[$param['nation']]) ? $this->r[$param['nation']] : 1 )),
  170. 'stock' => rand(1000, 5000),
  171. 'url' => $param['url'],
  172. 'comment' => false,
  173. ];
  174. }
  175. }
  176. }
  177. }
  178. else if ( $page > 1 )
  179. {
  180. if ( $key > ( $page - 1 ) * $limit && $key <= $page * $limit )
  181. {
  182. // $res = $this->curlGet($param['id'], $param['nation']);
  183. $res = $this->curlGet($param['id'], $param['nation'], $type=='alibaba'?'alibaba':'lazada');
  184. if ( $res )
  185. {
  186. if ( $res['error_code'] == '0000' )
  187. {
  188. $item = $res['item'];
  189. $products[] = [
  190. 'id' => $item['num_iid'],
  191. 'name' => $item['title'],
  192. 'pic_url' => $item['pic_url'],
  193. 'source' => $res['api_type'],
  194. 'rating' => 5,
  195. 'price' => $type=='alibaba'?$item['price']:single_price($item['price'] * ( isset($this->r[$param['nation']]) ? $this->r[$param['nation']] : 1 )),
  196. 'stock' => rand(1000, 5000),
  197. 'url' => $param['url'],
  198. 'comment' => false,
  199. ];
  200. }
  201. }
  202. }
  203. }
  204. }
  205. return response()->json(compact('products', 'pages', 'count', 'page', 'limit', 'type'), 200);
  206. }
  207. //产品开始采集入库
  208. public function bulk_product_collect_add( Request $request ) {
  209. set_time_limit(0);
  210. $ids = $request->ids;
  211. $category_id = $request->category_id;
  212. $type = $request->type ?? 'lazada';
  213. $category = Category::findOrFail($category_id);
  214. $url = array_column($ids, 'url');
  215. $ids = array_column($ids, NULL, 'id');
  216. $data = json_decode(file_get_contents(dirname(__FILE__) . '/apicj_input2.txt'), true);
  217. $params = [];
  218. $list = array_unique(array_intersect($url, $data['data']));
  219. $exits = Product::where('digital', 0)->where('in_storehouse', 1)->where('source', $type)->where('added_by', 'admin')->get();
  220. $has = array_column(collect($exits)->toArray(), 'barcode');
  221. foreach ( $list as $item )
  222. {
  223. $url = parse_url($item);
  224. $id = str_replace('.html', '', str_replace('/products/', '', $url['path']));
  225. $array = explode('-', $id);
  226. $host = explode('.', $url['host']);
  227. $nation = $host[count($host) - 2] . '.' . $host[count($host) - 1];
  228. $newId = str_replace('i', '', $array[count($array) - 1]);
  229. if ( $type == 'alibaba' ) $newId = explode('_', $newId)[count(explode('_', $newId)) - 1];
  230. if ( !in_array($newId, $has) )
  231. {
  232. $na = str_replace('lazada.', '', $nation);
  233. if ( $type == 'alibaba.com' ) $na = 'alibaba';
  234. $params[] = [
  235. 'id' => $newId,
  236. 'nation' => $na,
  237. ];
  238. }
  239. }
  240. foreach ( $params as $key => $param )
  241. {
  242. $res = $this->curlGet($param['id'], $param['nation'],$type=='alibaba'?'alibaba':'lazada');
  243. if ( $res )
  244. {
  245. if ( $res['error_code'] == '0000' )
  246. {
  247. DB::beginTransaction();
  248. $slug = Str::slug($res['item']['name']);
  249. $same_slug_count = Product::where('slug', 'LIKE', $slug . '%')->count();
  250. $slug_suffix = $same_slug_count ? '-' . $same_slug_count + 1 : '';
  251. $slug .= $slug_suffix;
  252. $productData = [
  253. 'added_by' => 'admin',
  254. 'name' => $res['item']['title'],
  255. 'category_id' => $category->id,
  256. 'brand_id' => '0',
  257. 'unit' => 'Pc',
  258. 'min_qty' => '1',
  259. 'tags' => '',
  260. 'barcode' => $res['item']['num_iid'],
  261. 'refundable' => '1',
  262. 'photos' => '', //570,569
  263. 'thumbnail_img' => '', //570
  264. 'video_provider' => 'youtube',
  265. 'video_link' => NULL,
  266. // 'unit_price' => $res['item']['price'] * ( isset($this->r[$param['nation']]) ? $this->r[$param['nation']] : 1 ),
  267. 'unit_price' => $type=='alibaba'?str_replace('$', '', $res['item']['price']):$res['item']['price'] * ( isset($this->r[$param['nation']]) ? $this->r[$param['nation']] : 1 ),
  268. 'discount' => '0',
  269. 'discount_type' => 'amount',
  270. 'earn_point' => '0',
  271. 'current_stock' => rand(1000, 5000),
  272. 'external_link' => NULL,
  273. 'external_link_btn' => NULL,
  274. 'description' => $res['item']['desc'],
  275. 'pdf' => NULL,
  276. 'meta_title' => $type=='alibaba'?$res['item']['title']:$res['item']['name'],
  277. 'meta_description' => $type=='alibaba'?$res['item']['desc_short']:$res['item']['name'],
  278. 'meta_img' => '570',
  279. 'shipping_type' => $res['item']['post_fee'] ? 'flat_rate' : 'free',
  280. 'low_stock_quantity' => '1',
  281. 'stock_visibility_state' => 'quantity',
  282. 'cash_on_delivery' => '1',
  283. 'est_shipping_days' => '3',
  284. 'user_id' => Auth::user()->id,
  285. 'approved' => 1,
  286. 'discount_start_date' => NULL,
  287. 'discount_end_date' => NULL,
  288. 'shipping_cost' => $res['item']['post_fee'] ? $res['item']['post_fee'] : 0,
  289. 'slug' => $res['item']['num_iid'],
  290. 'choice_attributes' => [],
  291. 'colors' => '[]',//'colors' => '["#7FFFD4","#A9A9A9"]',
  292. 'choice_options' => '[]',//{"attribute_id":"2","values":["Aaaa","Ssssss"]}
  293. 'attributes' => '[]',//转成JSON "2"
  294. 'published' => 0,
  295. 'in_storehouse' => 1,
  296. 'digital' => 0,
  297. 'source' => $type=='alibaba'?'alibaba':'lazada',
  298. ];
  299. //thumbnail_img图片下载
  300. $dir = 'download' . parse_url($res['item']['pic_url'])['path'];
  301. $mid = time() . mt_rand(100, 499);
  302. $client = new \GuzzleHttp\Client();
  303. $data = $client->request('get', $res['item']['pic_url'])->getBody()->getContents();
  304. Storage::disk('local')->put($dir, $data);
  305. $upload = new Upload;
  306. $upload->user_id = Auth::user()->id;
  307. $upload->file_size = 10240000;
  308. $upload->extension = 'jpg';
  309. $upload->type = 'image';
  310. $upload->file_original_name = $res['item']['pic_url'];
  311. $upload->file_name = $dir;
  312. $upload->mid = $mid;
  313. $upload->save();
  314. $productData['thumbnail_img'] = $upload->id;
  315. //photos下载
  316. $phones = [];
  317. $midPhotos = time() . mt_rand(100, 499);
  318. if ( $res['item']['props_img'] )
  319. {
  320. foreach ( $res['item']['props_img'] as $item )
  321. {
  322. $dir = 'download' . parse_url($item)['path'];
  323. $client = new \GuzzleHttp\Client();
  324. $data = $client->request('get', $item)->getBody()->getContents();
  325. Storage::disk('local')->put($dir, $data);
  326. $upload = new Upload;
  327. $upload->user_id = Auth::user()->id;
  328. $upload->file_size = 10240000;
  329. $upload->extension = 'jpg';
  330. $upload->type = 'image';
  331. $upload->file_original_name = $item;
  332. $upload->mid = $midPhotos;
  333. $upload->file_name = $dir;
  334. $upload->save();
  335. $phones[] = $upload->id;
  336. }
  337. }
  338. if ( $res['item']['item_imgs'] )
  339. {
  340. foreach ( $res['item']['item_imgs'] as $item )
  341. {
  342. $dir = 'download' . parse_url($item['url'])['path'];
  343. $client = new \GuzzleHttp\Client();
  344. $data = $client->request('get', $item['url'])->getBody()->getContents();
  345. Storage::disk('local')->put($dir, $data);
  346. $upload = new Upload;
  347. $upload->user_id = Auth::user()->id;
  348. $upload->file_size = 10240000;
  349. $upload->extension = 'jpg';
  350. $upload->type = 'image';
  351. $upload->file_original_name = $item['url'];
  352. $upload->mid = $midPhotos;
  353. $upload->file_name = $dir;
  354. $upload->save();
  355. $phones[] = $upload->id;
  356. }
  357. }
  358. $productData['photos'] = implode(',', $phones);
  359. //判断品牌
  360. if ($res['item']['brand']){
  361. $brand = Brand::where('name', $res['item']['brand'])->first();
  362. if ( $brand != NULL )
  363. {
  364. $productData['brand_id'] = $brand->id;
  365. }
  366. else
  367. {
  368. $brand = new Brand;
  369. $brand->name = $res['item']['brand'];
  370. $brand->meta_title = $res['item']['brand'];
  371. $brand->meta_description = $item['brand'];
  372. $brand->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $res['item']['brand'])) . '-' . Str::random(5);
  373. $brand->logo = '';
  374. $brand->save();
  375. $brand_translation = BrandTranslation::firstOrNew([
  376. 'lang' => env('DEFAULT_LANGUAGE'),
  377. 'brand_id' => $brand->id,
  378. ]);
  379. $brand_translation->name = $res['item']['brand'];
  380. $brand_translation->save();
  381. $productData['brand_id'] = $brand->id;
  382. }
  383. }
  384. $product = Product::create($productData);
  385. //属性计算
  386. $choice_attributes = $choice_options = [];
  387. if ( $res['item']['props_list'] )
  388. {
  389. foreach ( $res['item']['props_list'] as $key => $re )
  390. {
  391. $item = explode(':', $re);
  392. $attribute = Attribute::where('name', $item[0])->firstOrNew();
  393. $attribute->name = $item[0];
  394. $attribute->save();
  395. $attribute_translation = AttributeTranslation::firstOrNew([
  396. 'lang' => env('DEFAULT_LANGUAGE'),
  397. 'attribute_id' => $attribute->id,
  398. ]);
  399. $attribute_translation->name = $attribute->name;
  400. $attribute_translation->save();
  401. $attribute_value = AttributeValue::where('attribute_id', $attribute->id)->where('value', $item[1])->firstOrNew();
  402. $attribute_value->attribute_id = $attribute->id;
  403. $attribute_value->value = $item[1];
  404. $attribute_value->save();
  405. $choice_attributes[$attribute->id] = $attribute->id;
  406. $choice_options[$attribute->id]['attribute_id'] = $attribute->id;
  407. $choice_options[$attribute->id]['values'][$key] = $attribute_value->value;
  408. }
  409. }
  410. $data = [
  411. 'colors_active' => '0',
  412. 'choice_no' => array_values($choice_attributes),
  413. 'unit_price' => $type=='alibaba'?str_replace('$', '', $res['item']['price']):$res['item']['price'] * ( isset($this->r[$param['nation']]) ? $this->r[$param['nation']] : 1 ),
  414. 'sku' => NULL,
  415. 'current_stock' => rand(1000, 5000),
  416. 'product_id' => $product->id,
  417. ];
  418. $collection = collect($data);
  419. $options = ProductUtility::get_attribute_options($collection);
  420. //Generates the combinations of customer choice options
  421. $combinations = Combinations::makeCombinations($options);
  422. $variant = '';
  423. if ( count($combinations[0]) > 0 )
  424. {
  425. $product->variant_product = 1;
  426. $product->save();
  427. foreach ( $combinations as $key => $combination )
  428. {
  429. $str = ProductUtility::get_combination_string($combination, $collection);
  430. $product_stock = new ProductStock();
  431. $product_stock->product_id = $product->id;
  432. $product_stock->variant = $str;
  433. // $product_stock->price = $res['item']['price'] * ( isset($this->r[$param['nation']]) ? $this->r[$param['nation']] : 1 );
  434. $product_stock->price =$type=='alibaba'?str_replace('$', '', $res['item']['price']):single_price($res['item']['price'] * ( isset($this->r[$param['nation']]) ? $this->r[$param['nation']] : 1 ));
  435. $product_stock->sku = '';
  436. $product_stock->qty = rand(1000, 5000);
  437. $product_stock->image = $product->thumbnail_img;
  438. $product_stock->save();
  439. }
  440. }
  441. else
  442. {
  443. unset($collection['colors_active'], $collection['colors'], $collection['choice_no']);
  444. $qty = $collection['current_stock'];
  445. $price = $collection['unit_price'];
  446. unset($collection['current_stock']);
  447. $data = $collection->merge(compact('variant', 'qty', 'price'))->toArray();
  448. ProductStock::create($data);
  449. }
  450. //开始检测是否开启评论采集
  451. if ( isset($ids[$param['id']]) && $type=='lazada')
  452. {
  453. if ( $ids[$param['id']]['comment'] == '1' )
  454. {
  455. $cRes = $this->curlGetCom($param['id'], $param['nation']);
  456. // dd($cRes);
  457. if ( $cRes )
  458. {
  459. if ( $cRes['error_code'] == '0000' )
  460. {
  461. $items = $cRes['items']['item'];//评论列表
  462. foreach ( $items as $item )
  463. {
  464. //添加用户信息
  465. $addUser = User::where('name', $item['display_user_nick'])->where('avatar', $item['head_pic'])->firstOrNew();
  466. $addUser->name = $item['display_user_nick'];
  467. $addUser->avatar = $item['head_pic'];
  468. $addUser->password = '$2y$10$cCzxgRHlITBgIrTHdzys8.TqtwV42.Fs2PakWtfHw0wnOt0obs/ai';
  469. $addUser->email_verified_at = date("Y-m-d H:i:s");
  470. $addUser->user_type = 'customer';
  471. $addUser->save();
  472. $addUser->email = $addUser->id . rand(111111, 999999) . "@qq.com";
  473. //开始添加评论
  474. if ( $item['rate_content'] )
  475. {
  476. $review = new Review;
  477. $review->product_id = $product->id;
  478. $review->user_id = $addUser->id;
  479. $review->rating = $item['rating'];
  480. $review->comment = $item['rate_content'];
  481. $review->viewed = '0';
  482. $review->save();
  483. }
  484. if ( Review::where('product_id', $product->id)->where('status', 1)->count() > 0 )
  485. {
  486. $product->rating = Review::where('product_id', $product->id)->where('status', 1)->sum('rating') / Review::where('product_id', $product->id)->where('status', 1)->count();
  487. }
  488. else
  489. {
  490. $product->rating = 0;
  491. }
  492. $product->save();
  493. }
  494. }
  495. }
  496. }
  497. }
  498. DB::commit();
  499. }
  500. }
  501. }
  502. return 1;
  503. }
  504. public function product_collect_update( Request $request ) {
  505. if ( $request->id )
  506. {
  507. Product::where('added_by', 'admin')->where('id', $request->id)->update([ 'in_collect' => $request->in_collect ]);
  508. return 1;
  509. }
  510. return 0;
  511. }
  512. public function product_collect_move_out( $id ) {
  513. if ( $id )
  514. {
  515. Product::where('added_by', 'admin')->where('id', $id)->update([ 'in_collect' => 0 ]);
  516. }
  517. return back();
  518. }
  519. public function product_collect_order_free_up( $id ) {
  520. if ( $id )
  521. {
  522. product_collect_order_free_up($id);
  523. }
  524. return back();
  525. }
  526. public function bulk_product_collect_remove( Request $request ) {
  527. if ( $request->id )
  528. {
  529. Product::where('added_by', 'admin')->whereIn('id', $request->id)->update([ 'in_collect' => 0 ]);
  530. return 1;
  531. }
  532. return 0;
  533. }
  534. protected function curlGet( $productId, $nation, $type = 'lazada' ) {
  535. $hosts = [
  536. 'https://api-gw.onebound.cn',
  537. 'https://api-1.onebound.cn',
  538. 'https://api-2.onebound.cn',
  539. 'https://api-3.onebound.cn',
  540. 'https://api-4.onebound.cn',
  541. ];
  542. foreach ( $hosts as $host )
  543. {
  544. // 请求示例 url 默认请求参数已经URL编码处理
  545. $url = $host . "/lazada/item_get/?key=" . $this->key . "&nation=" . $nation . "&secret=" . $this->secret . "&num_iid=" . $productId;
  546. if ($type=='alibaba')$url = $host . "/alibaba/item_get/?key=" . $this->key . "&secret=" . $this->secret . "&num_iid=" . $productId;
  547. $headerArray = [ "Content-type:application/json;charset=UTF-8", "Accept:application/json" ];
  548. $ch = curl_init();
  549. curl_setopt($ch, CURLOPT_URL, $url);
  550. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  551. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  552. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  553. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
  554. $output = curl_exec($ch);
  555. curl_close($ch);
  556. $output = json_decode($output, true);
  557. if ( isset($output['error_code']) )
  558. {
  559. if ( $output['error_code'] == '0000' )
  560. {
  561. return $output;
  562. }
  563. }
  564. }
  565. }
  566. protected function curlGetCom( $productId, $nation ) {
  567. $hosts = [
  568. 'https://api-gw.onebound.cn',
  569. 'https://api-1.onebound.cn',
  570. 'https://api-2.onebound.cn',
  571. 'https://api-3.onebound.cn',
  572. 'https://api-4.onebound.cn',
  573. ];
  574. foreach ( $hosts as $host )
  575. {
  576. // 请求示例 url 默认请求参数已经URL编码处理
  577. $url = $host . "/lazada/item_review/?key=" . $this->key . "&nation=" . $nation . "&secret=" . $this->secret . "&page=1&num_iid=" . $productId;
  578. $headerArray = [ "Content-type:application/json;charset=UTF-8", "Accept:application/json" ];
  579. $ch = curl_init();
  580. curl_setopt($ch, CURLOPT_URL, $url);
  581. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  582. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  583. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  584. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
  585. $output = curl_exec($ch);
  586. curl_close($ch);
  587. $output = json_decode($output, true);
  588. if ( isset($output['error_code']) )
  589. {
  590. if ( $output['error_code'] == '0000' )
  591. {
  592. return $output;
  593. }
  594. }
  595. }
  596. return '';
  597. }
  598. }