Simple analysis of how PHP realizes the _php instance of the second-function common in app

Source: Internet
Author: User

Objective

Imagine, we usually send a friend circle, n picture with text, swish of a bit to send out, do not bring any kind of muddy, experience feeling good ~.

But we stopped to think in a technical way, is that possible? Some of the 2G network is up to dozens of K speed, we have a picture of a few m, even if the compression has hundreds of K, how can the instant message?

Now think about it, is it a little weird?

In fact, many social software (microblogs, micro-letters) are all using a second-hair mechanism. He's not really going to send it first and then send it over and tell you to send it successfully, but directly to tell you to send success, and then secretly to upload your hair in the background, so when the speed is bad we will often find a phenomenon, just started out of the circle of friends is very normal, but after a few minutes, prompted us to send failure! This is very awkward ah, clearly at the beginning you did not say Ah, to the most critical time you tell me not to.

Don't be tall technology scare, nothing but is to do some small skills, really very basic, but also really very practical.

Before discussing the technology, we first declare some initial conditions.

1. Some special modifications have been made to the database table structure: Friends circle the Content table has a special field status, the status value in two cases, the value can be 1 or 2,
A value of 1 is not published by the Circle of friends. A value of 2 is a circle of friends has been published. (temporarily do not understand why the students do so, you can continue to read, later will explain)

2. The second function of this article refers to the use of a picture, because there are pictures, pictures upload too slow, so need to use the second mechanism, but there is no picture of pure text, it is not much necessary, because the text transmission is very low, according to the normal process to send.

3. The code for this article is based on the PHALAPI framework, the syntax is relatively simple, have an ORM operation experience students should be able to read

4. This article mainly explains app's second hair function, the web side is not particularly need this function, because the modern network is enough for our PC to send many many pictures at once (10m/s,20m/s)

The general direction discusses the entire implementation process:

The client invokes the Publishing API, the service side publishes the content (publish.php), and if there is a picture, the client also calls an additional upload API (upload.php), in this upload API ( upload.php) has not finished the work, the client will tell you the success of the release (in fact, there is no upload is completed, there is a process behind the desperately to help you upload it), and then the client will send your text and pictures of the temporary stitching to show you see (currently only you can see, You can't see it in your friends ' circle. Then wait for the upload API (upload.php) Results/Of course, may upload timeout (usually in a minute), if successful, then upload smoothly, failure to send a failure, but in the waiting for the result of the minute, He'll make you feel like you've already sent it, unless the upload fails to remind you later.

So let's analyze this mechanism at a technical level:

When we click the Send key in the top right-hand corner, there are two processes that start at the same time, one of which is to help you upload the text and tell you that it has been sent successfully (publish.php), and another process is to secretly upload the pictures you sent (upload.php ), the specific code is as follows:

publish.php

<?php
 
//normal access to data (text, pictures, location information, etc.) ...
 
Code ...
 
Code ...
 
If a picture is not published (Status 1), no picture is immediately released (status 2)
 
///If a picture is told to the client by the return identifier, Let him quickly to invoke the real upload logic upload.php, we only put the most basic text upload good, and then set more than a status ~
 
$status = ($pic _num > 0)? 1:2;
 
Splicing data
 
$where _data = Array ("status" => $status)
 
//Data Warehousing
 
DI ()->notorm-> friends->insert ($ Where_data);
 
? >

Do you see the occult? We made a judgment on the status field for the storage, which is 1 (unpublished) and 2 (published), so what should we do when we read the data (list.php)?

So the presentation page is like this:

lists.php

<?php
 
//code.
 
Get text information
 
Code ...
 
Get picture Info
//(It gets the f_id of the current user's pic table (that is, the picture of the friend Circle), the most critical place is the WHERE condition
$data = DI ()->notorm->pic->select (' F_ Id ')->where ("Status > 1 OR (status = 1 && u_id = {$u _id})")->->fetchall ();
 
 
Code ...
 
? >

The where condition here is the most critical part of the second-send mechanism:

status is greater than 1 (published) or equal to 1 (unpublished), (tips:status In the case of a picture with the default value of 1) but it belongs to the current user published content, can be read out, there is a wonderful phenomenon Is that, in any case, our own circle of friends, we can always read, but others are not necessarily (because if there is a picture, you need to call another process to upload the picture, and then in that process to change status to 2)

Then there is a final key point, is responsible for uploading the image of the process (upload.php), this is the real upload of the image of the logic,

There are several pictures, so upload.php will be called several times

Change the field status of the picture row to 2 after each upload success

upload.php

<?php
 
//code.
 
Upload the picture to the server directory
 
//Get the long pass result identification, change the status
 
If (upload succeeded) {
 
//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 above several operations (first is publish.php, if there is a picture upload then call upload.php, show the time is list.php).

Do not know that we do not see the doorway, and we usually write the release function is different, upload upload.php function is independent out, modified release publish.php will use the fastest speed of your text content into the database, And if there is a picture of the content, he will invoke the upload API upload.phpalone.

The most important thing is to do some tricks in the display and make sure that you can see what you are sending.

Well, the above is the entire content of this article, I hope to learn from PHP help you, but also please continue to pay attention to the cloud habitat community.

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.