


Optimize client calls to server interfaces to reduce request data capacity
Due to project needs, it is necessary to record the user's reservation time in the next three days, and each reservation period is 1 hour.
For example: 00:00:00~00:59:59 or 01:00:00~01:59:59 etc. is a reservation period
The front-end code is as follows:
nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <meta> <title> 提交预约日期 </title> <style> .title{color:#FF0000;} .topic{font-size:18px; font-weight:bold;} </style> <script></script> <script> function fsubmit(){ var timetable = []; $("#myform input[type=checkbox]").each(function(){ if(this.checked){ timetable.push($(this).val()); } }); if(timetable.length==0){ alert('请选择预约时间'); return false; } $.post("http://localhost/server.php",{timetable:timetable}).done(function(data){ if(data['success']==true){ alert('提交成功'); }else{ alert('提交失败'); } }); } </script> <p>请选择预约时间:</p>
The back-end code is as follows:
<?php $data = $_POST['timetable']; file_put_contents('timetable.log', json_encode($data)); header('content-type:applicaiton/json;charset=utf8');echo json_encode(array('success'=>true));?>
For example, to submit a user's 3 Day reservation data, reservations for all time periods (select all)
The request data received by the backend is as follows:
["2015-05-28 00:00:00","2015-05-28 01:00:00","2015-05-28 02:00:00","2015-05-28 03:00:00","2015-05-28 04:00:00","2015-05-28 05:00:00","2015-05-28 06:00:00","2015-05-28 07:00:00","2015-05-28 08:00:00","2015-05-28 09:00:00","2015-05-28 10:00:00","2015-05-28 11:00:00","2015-05-28 12:00:00","2015-05-28 13:00:00","2015-05-28 14:00:00","2015-05-28 15:00:00","2015-05-28 16:00:00","2015-05-28 17:00:00","2015-05-28 18:00:00","2015-05-28 19:00:00","2015-05-28 20:00:00","2015-05-28 21:00:00","2015-05-28 22:00:00","2015-05-28 23:00:00","2015-05-29 00:00:00","2015-05-29 01:00:00","2015-05-29 02:00:00","2015-05-29 03:00:00","2015-05-29 04:00:00","2015-05-29 05:00:00","2015-05-29 06:00:00","2015-05-29 07:00:00","2015-05-29 08:00:00","2015-05-29 09:00:00","2015-05-29 10:00:00","2015-05-29 11:00:00","2015-05-29 12:00:00","2015-05-29 13:00:00","2015-05-29 14:00:00","2015-05-29 15:00:00","2015-05-29 16:00:00","2015-05-29 17:00:00","2015-05-29 18:00:00","2015-05-29 19:00:00","2015-05-29 20:00:00","2015-05-29 21:00:00","2015-05-29 22:00:00","2015-05-29 23:00:00","2015-05-30 00:00:00","2015-05-30 01:00:00","2015-05-30 02:00:00","2015-05-30 03:00:00","2015-05-30 04:00:00","2015-05-30 05:00:00","2015-05-30 06:00:00","2015-05-30 07:00:00","2015-05-30 08:00:00","2015-05-30 09:00:00","2015-05-30 10:00:00","2015-05-30 11:00:00","2015-05-30 12:00:00","2015-05-30 13:00:00","2015-05-30 14:00:00","2015-05-30 15:00:00","2015-05-30 16:00:00","2015-05-30 17:00:00","2015-05-30 18:00:00","2015-05-30 19:00:00","2015-05-30 20:00:00","2015-05-30 21:00:00","2015-05-30 22:00:00","2015-05-30 23:00:00"]
Request data content- length:2879
Using this method to request a larger data capacity will affect the response time.
Improvement method 1: Convert the DateTime format time to a timestamp and then submit
The front-end code is modified as follows: Modify the javascript fsubmit method
<script type="text/javascript"> function fsubmit(){ var timetable = []; $("#myform input[type=checkbox]").each(function(){ if(this.checked){ // 把时间转为时间戳 var time = $(this).val(); var timestamp = Date.parse(new Date(time)); timestamp = timestamp / 1000; timetable.push(timestamp); } }); if(timetable.length==0){ alert('请选择预约时间'); return false; } $.post("http://localhost/server.php",{timetable:timetable}).done(function(data){ if(data['success']==true){ alert('提交成功'); }else{ alert('提交失败'); } }); } </script>
The request data received by the backend is as follows:
["1432742400","1432746000","1432749600","1432753200","1432756800","1432760400","1432764000","1432767600","1432771200","1432774800","1432778400","1432782000","1432785600","1432789200","1432792800","1432796400","1432800000","1432803600","1432807200","1432810800","1432814400","1432818000","1432821600","1432825200","1432828800","1432832400","1432836000","1432839600","1432843200","1432846800","1432850400","1432854000","1432857600","1432861200","1432864800","1432868400","1432872000","1432879200","1432882800","1432886400","1432890000","1432893600","1432897200","1432900800","1432904400","1432908000","1432911600","1432915200","1432918800","1432922400","1432926000","1432929600","1432933200","1432936800","1432940400","1432944000","1432947600","1432951200","1432954800","1432958400","1432962000","1432965600","1432969200","1432972800","1432976400","1432980000","1432983600","1432987200","1432990800","1432994400","1432998000"]
1916
After receiving it, the backend can convert the timestamp into datetime format for use
starting from right to left: 00:00, 01:00, 02:00 ,03:00 and so on, if there is a selected time period, it will be 1, otherwise it will be 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 忽略 忽略 忽略 忽略 忽略 忽略 忽略 忽略 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
1010101010000000000, that is, decimal 349184 = 2 to the 10th power 2 to the 12th power 2 14 times 2 to the 16th power 2 to the 18th power
The front-end code is modified as follows: Modify the javascript fsubmit method
<script type="text/javascript"> function fsubmit(){ var timetable = []; var tmp = {}; $("#myform input[type=checkbox]").each(function(){ if(this.checked){ // 拆分日期与时间 var datetime = $(this).val(); var datetime = datetime.split(' '); var day = datetime[0]; var time = parseInt(datetime[1].substring(0,2)); // 创建日期与时间数组 if(typeof(tmp[day])=='undefined'){ tmp[day] = []; } tmp[day].push(time); } }); // 合拼数据,转换格式 for(day in tmp){ // 根据数据集合,合拼,创建10进制数据 var time = tmp[day]; var timeint = 0; for(var i=0; i<time.length; i++){ timeint += Math.pow(2,time[i]); } timetable.push(day+' '+timeint); } if(timetable.length==0){ alert('请选择预约时间'); return false; } $.post("http://localhost/server.php",{timetable:timetable}).done(function(data){ if(data['success']==true){ alert('提交成功'); }else{ alert('提交失败'); } }); } </script>
The request received by the backend The data is as follows:
["2015-05-28 16777215","2015-05-29 16777215","2015-05-30 16777215"]
107
Use this Method, no matter how many reservation time periods are selected in a day, there will only be one record, which greatly reduces the size of submitted data.
Backend method of converting decimal to time: 1. Convert decimal to binary and reverse
2. Loop through each bit and record the value of 1
3. Save the record
The code is as follows:
<?php$datetime = '2015-05-28 1398016';list($day, $time) = explode(' ', $datetime);$bintime = decbin($time);$bintime = strrev($bintime);$result = array();for($i=0,$len=strlen($bintime); $i<$len; $i++){ if(substr($bintime, $i, 1)==1){ $result[] = $day.' '.str_pad($i, 2, '0', STR_PAD_LEFT).':00:00'; } } print_r($result);?>
Output:
Array( [0] => 2015-05-28 08:00:00 [1] => 2015-05-28 10:00:00 [2] => 2015-05-28 12:00:00 [3] => 2015-05-28 14:00:00 [4] => 2015-05-28 16:00:00 [5] => 2015-05-28 18:00:00 [6] => 2015-05-28 20:00:00)
Summary: This article provides a method that can convert large amounts of data into small-capacity data transmission, but it requires adding more operations. Therefore, in actual development, it is necessary to judge whether the algorithm of exchanging time for space or space for time is suitable based on actual needs.
This article explains how to optimize the client to call the server interface to reduce the request data capacity. For more related content, please pay attention to the PHP Chinese website. Related recommendations:mysql order by rand() efficiency optimization method
php Get the start date and end date All dates between
php method to get a random combination from the specified number
The above is the detailed content of Optimize client calls to server interfaces to reduce request data capacity. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
