Analysis on how php implements the second function commonly used by apps, and analysis on how php implements apps

Source: Internet
Author: User

Analysis on how php implements the second function commonly used by apps, and analysis on how php implements apps

Preface

Imagine that, when we usually share a circle of friends, N images with texts are sent out at a glance, without any drag or drop, and the experience is great ~.

But let's stop and use technical means to think about it. Is that possible? Some 2G networks have a speed of dozens of K at most. Our images are just a few M at a time, even if they are compressed by hundreds of K, how can we send messages instantly?

Think about it now, isn't it a little strange ~

In fact, many social networking software (Weibo) uses a second-sending mechanism. Instead of sending the message first and then telling you that the message was sent successfully, he directly told you that the message was sent successfully, and then secretly uploaded the content you sent in the background, therefore, when the network speed is poor, we often find a phenomenon. The circle of friends that was just sent out is normal at the beginning, but after a few minutes, we are prompted to fail to send! This is very embarrassing. You didn't say anything at the beginning. You told me when it was the most important thing ..

Don't be scared by advanced technologies. It's just a little bit of skill. It's really basic, but it's really practical.

Before discussing the technology, declare some initial conditions.

1. Some special changes have been made to the database table structure: the content table in the circle of friends has a special field status. The status value can be 1 or 2,
If the value is 1, it is not released in the circle of friends. The value 2 indicates that the circle of friends has been published. (If you do not understand why this is done for the moment, you can continue reading it. I will explain it later)

2. the second sending function of this article refers to the use of images, because the upload of images is too slow, so the second sending mechanism is required, but there is no plain text in the image, it is not necessary because the amount of text transmitted is very low and it can be sent according to the normal process.

3. The code in this article is based on the PhalApi framework and the syntax is relatively simple. Anyone with experience in ORM operations should be able to understand it.

4. This article mainly describes the APP's second sending function. The WEB side does not need this function because the modern network is enough to send many images (10 M/s, 20 M/s) at a time on our PC)

The general direction is to discuss the entire execution process:

The client calls the publish API, and the server publishes the content (Publish. php), If there is an image, the client also needs to call an additional upload API (Upload. php), In this upload API (Upload. php) Before the job is completed, the client will directly tell you that the release is successful (In fact, the current upload is not complete, and a process is desperately trying to help you upload it ), then, the client will splice your text and images for display (only you can see them, but others in your circle of friends can't see them) and wait for the upload API (Upload. php) Results/Of course, the upload may also time out (usually results will be generated within one minute). If the upload succeeds, the upload will be successful. If the upload fails, the sending will fail, but within one minute of waiting for the result, he will first remind you that you have already sent the message, unless the upload fails.

Let's analyze this mechanism at the technical level:

After we click the send key in the upper-right corner, two processes are started at the same time. One process uploads text for you and tells you that the message has been sent successfully (Publish. php), Another process is to secretly upload the image you sent (Upload. php), The Code is as follows:

Publish. php

<? Php // get data normally (text, image, location information, etc )... Code... Code... // Judge. If there is an image, it is not published (status is 1), and if there is no image, it is published immediately (status is 2) // if there is an image, the client will be notified by returning the identifier, so that he can call the real upload logic upload. php. We only need to upload the most basic text and set another status ~ $ Status = ($ pic_num> 0 )? 1: 2; // concatenate data into the database $ where_data = array ("status" => $ status) // data into the database DI () -> notorm-> friends-> insert ($ where_data);?>

Is that mysterious? WeStatusThe field is judged once. There are two situations: 1 (not released) and 2 (released). What should we do when reading data (List. php)?

The display page is like this:

Lists. php

<? Php // code .. // get the text Code .. // obtain the image information // (it is used to obtain the f_id in the pic table of the current user (that is, to obtain the image of the circle of friends). The most important thing is the where condition $ data = DI () -> notorm-> pic-> select ('f _ id')-> where ("status> 1 OR (status = 1 & u_id = {$ u_id })") -> fetchAll (); // code...?>

HereWhereThe condition is the most critical aspect of the second sending mechanism:

StatusGreater than 1 (published) or equal to 1 (unpublished ),(Tips:StatusIn the case of images, the default value is 1) but the content published by the current user can be read. This is a wonderful phenomenon, we can always read the images in our own circle of friends, but others may not. (if there are images, we also need to call another process to upload images, then change the status to 2 in that process)

The last key point is the process responsible for uploading images (Upload. php). This is the logic for uploading images,

There are several images.Upload. phpWill be called several times

After each upload is successfulStatusChange to 2

Upload. php

<? Php // Code .. // upload the image to the server directory // obtain the long-pass result ID and change the status If (uploaded successfully) {// change status back to 2 $ status_data = array ("status" => 2); DI ()-> notorm-> pic-> select ('U _ id ') -> where ('U _ id, $ u_id) --> update ($ status_data);} else {Code... }

After the preceding operations (firstPublish. phpIf an image is uploaded, callUpload. phpIS DISPLAYEDList. php).

I don't know if you can see the portal is missing. What is different from the publishing function we usually write is that uploadUpload. phpThe function is released independently.Publish. phpThe fastest way to store your text content in the database, and if there is image content, it will independently call the upload APIUpload. php.

The most important thing is to do some tips during display so that you can ensure that you can see what you are sending.

Well, the above is all the content in this article. I hope it will help you learn PHP. Please stay tuned to the help house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.