In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use php to achieve automatic receipt of goods, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Php to achieve automatic receiving methods: 1, through the scheduled task of linux to confirm the receipt of the order information query; 2, the order information will be stored on the redis; 3, the implementation of the corresponding script.
This article operating environment: linux5.9.8 system, PHP7.1 version, DELL G3 computer
How to receive goods automatically by php?
PHP realizes the redis queue for automatic confirmation and receipt of e-commerce orders
I. scene
On the e-commerce platform made before, after receiving the goods, most users will not take the initiative to click to confirm the receipt of the goods, resulting in all kinds of complaints to the merchants, so according to the demand, make an order to automatically confirm the receipt of the goods x days after delivery. The so-called order automatic confirmation of receipt is the execution of a update statement at a specific time to change the status of the order.
Second, train of thought
The clunkiest approach is to schedule tasks through the linux background, query eligible orders, and then update. Ideally, this is fine if there are orders that require update every minute. However, the platform is too small, and most of the seller's delivery time is intensive and will not be scattered every 24-hour minute. Then, for scheduled tasks, there are too many queries, which is not suitable. Here, you can first store the order information that will automatically confirm receipt to other media, such as redis,memcache,rabbitmq, and then execute the script to get the order information from the previous media to judge, here can greatly reduce the query pressure on the database.
Producers of redis queues
In this regard, we choose to query the order information that is about to confirm receipt through the scheduled task of linux at 2: 00 in the morning every day, and then store it on redis. The queue we selected on redis is characterized by first-in, first-out. When querying the order, the previous data is sorted by the delivery time, so the order that leaves the queue first must be the order closest to the specified automatic receiving time. The code is as follows
$successCount=0;$failCount=0;$screen_time = 36002409ramp / set screening days $data = array (); $now_time = time (); / / query data that meet the requirements $sql= "select id,send_time as deliver_time from `order` where is_send=1 and is_del=0 and is_cancel=0 and is_token=0 and send_time > 0 and send_time + {$screen_time}
< $now_timeorder by send_time asc";$res = $con->Query ($sql); / / record and clear while when there is still data in the queue ($redis- > LLEN ('auto_recevice_order')) {$txt =' execution time: '.date (' Y-m-d Y-m-d'). Information:'. $redis- > RPOP ('auto_recevice_order'); file_put_contents ('. / autoToken/fail_log.txt',$txt. "\ r\ n" .PHP _ EOL,FILE_APPEND); $failCount++ } / / re-populate the data into the queue while ($row = $res- > fetch_assoc ()) {$successCount++; $redis- > LPUSH ('auto_recevice_order',json_encode ($row1));} $con- > close (); $success=date (' Y-m-d Hlaviss').': [push successful]: this successful push data:'. $successCount.' Record the last processing failure data:'. $failCount. "Article\ r\ n"; file_put_contents ('. / success_log.txt',$success. "\ r\ n" .PHP _ EOL,FILE_APPEND)
Consumers of the redis queue
The consumers in the queue do not do it through the scheduled task of linux, but use the screen+php cli mode of linux to execute the php script. Consumers only need to constantly read the order information from the queue, then judge the delivery time in the order information, and execute the update statement if they meet the requirements of automatic receiving. At the same time, if the receiving time is not reached, and when there is a large gap between the receiving time and the receiving time, you can let the php script hibernate sleep for a certain number of time, which adjusts the design by itself, and the orders that do not meet the time requirements need to be re-pushed to the redis queue, and it is also at the top of the queue. So that we can get it next time. The code is as follows:
While (true) {if ($I% 30cm / 0) {usleep (10); / / prevent the while cycle from causing excessive CPU utilization} if ($redis- > LLEN ('auto_recevice_order')) {$data = json_decode ($redis- > RPOP (' auto_recevice_order')); $id = (int) $data- > id;// converts the data into shaping $deliver_time = (int) $data- > deliver_time / / convert data to shaping $res1 = $res2 = false;$now_time = time (); if (($deliver_time+$set_time) query ($sql1); / / Update data $rows = mysqli_affected_rows ($con); if ($rows) {$ip = $this- > getIp () $sql2 = "insert into `order_ log` (`order_ id`, `log_ msg`, `log_ ip`, `log_ role`, `log_ user`, `log_order_ state`, `log_ time`) VALUES ($id,' system automatic receiving','$ip',' system', 'server', 'receiving', $now_time)"; / / write to the order log $res2 = $con- > query ($sql2) / / add log data}} if ($res1==false) {/ / reinsert the data that does not meet the criteria into the queue $redis- > RPUSH ('auto_recevice_order',json_encode (' id'= > $id,'deliver_time'= > $deliver_time));}} $iComplete;}
To execute the php script, you need to use linux's screen or supervisor or nohup daemons.
Third, thinking
With the growth of business, there are multiple orders to be processed in the queue in the same second, and when only one relevant order information can be extracted from the queue at a time, the mode of one producer and multiple consumers can be adopted. a locking mechanism can be used to ensure that a message can only reach one consumer. When the redis data reaches a certain amount, the execution frequency and corresponding conditions of the producer can also be adjusted appropriately.
After reading the above, have you mastered how to use php to realize automatic receiving? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.