Solve the problem of the stability of PHP Snoopy remote Data acquisition

Source: Internet
Author: User
Solve the stability problem of PHP Snoopy remote Data acquisition

?

The use of php?snoopy in the acquisition of remote data, often encountered remote site instability or network factors, resulting in read data failure or return time is too long, this article focuses on the general solution.

In the past time, the use of Shenyang traffic violation query network interface, made a simple violation of the query system, and before the data remote access to cache, the start of the cache time is 3 days (because Shenyang traffic violation query network data updated once a week). This will run for 3 months, through a long period of observation, found that traffic police data source is not stable, 7 days a week about 2 days of the site is in an inaccessible situation, in the case of cache failure, often cause long wait time (about 30s), and finally return to empty query results.

?

This problem has a great impact on the user experience, I have also considered a long time, the solution is as follows:

1. The request of the interface is judged;

2. Set a reasonable time-out. (approximately 2-5s will definitely return data depending on the remote site response speed)

3. Writes the result of the last successful query to the data table, and invokes the last successful query record when the interface fails.

?

Combined with the above methods, the basic problem can be solved. The running process is as follows:

1. User input information and submit query request

2. The server side detects whether the query data is cached and valid, and returns directly if it exists and is valid (the most resources, not the remote access)

3. If the cache does not exist or fails, call the remote fetch method

4. The remote acquisition method uses Php?snoopy to obtain the remote data, which needs to set the time-out and determine the return status and whether to timeout.

$snoopy = new Snoopy; $snoopy->read_timeout=4;  Read time-out period

?

if ($snoopy->status>0&& $snoopy->status== ' &&! $snoopy->timed_out) {    // Here is the processing logic for the Read success}else{    //Here is the processing logic of the remote interface when something goes wrong}

? first of all, $snoopy->status is a remote return status code, the successful return should be 200, in other cases should be wrong, and the valid request status should be greater than 0, $snoopy->timed_ The value is False if the out is not timed out, and the return value should be true if the read request times out.

5. If the interface request does not time out, the latest data processing is done directly, and the last query results related to the data are deleted, and the most recent query results are written to the database.

/** * Write last query record * @param type $cache The latest query result passed by _data */Private Function Write_last_info ($cache _data) {        $car _info= $cache _data[' car_info ';        $M =m (' Sycar ');        $info = $M->where ("wz_car_no= ' {$car _info[' wz_car_no ']} ')->find ();            if (empty ($info)) {$car _info[' wz_datetime ']= get_date_full (Time ());            $car _info[' Wz_car_memo ']= session (' Memo ');        $M->add ($car _info);                }else{if ($info [' Wz_car_memo ']!=session (' Memo ')) {$info [' Wz_car_memo ']=session (' Memo ');            $M->save ($info);        }} unset ($info);                                   Unset ($M);        $M =m (' Sycarlastweizhang ');                 $M->where ("wz_car_no= ' {$car _info[' wz_car_no ']} ')->delete ();            foreach ($cache _data[' Weizhang_info ') as $key = + $value) {$value [' wz_car_no ']= $car _info[' wz_car_no '];            $value [' Input_time ']= get_date_full (Time ()); $M->add($value);                  } unset ($M);        unset ($cache _data);    Unset ($M); }

? 6. Read the results of the last query in the case of a remote interface acquisition failure

$weizhang _info=m (' Sycarlastweizhang ')->where ("wz_car_no= ' {$car _no} '")->order ("Wz_datetime")->select () if (Empty ($weizhang _info)) {   $is _cache=false;   $pop _info= ' Remote Data acquisition failed, please make a query later! '; }                     $cache _data = Array (       ' pop_info ' = = $pop _info,       ' car_info ' + $car _info,//Get the car information returned by the remote       Weizhang_info ' + $weizhang _info,//Get an array of illegal records returned by the remote    );

? By the way, if in the last Query results table, the query information does not exist, then you can only regret to return "remote Data acquisition failed, please you later in the query" error prompt.? This place can also output other prompt statements.

?

This article has basically solved the remote site interface instability impact of user experience issues, more perfect processing logic needs to continue to improve. I hope this article ideas can help you.

From:http://www.9it.me/article-128.html

?

?

  • Related Article

    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.