Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

php - Google Search Autocomplete/Autosuggest Function Slow

I have an issue with the Autocomplete respons on a Search Engine base on Google CSE. it Starts autocompleting from the 3rd letter and it takes at least 2-3 seconds to load the list. which is not the case with other Custom Search Scripts.

Website : https://searchpal.com

Please advice, if any of the below code or suggestqueries URL is causing this slowliness ?

Thank you all

Code as follows

/**
 * Suggests queries as the user types from Google auto-complete
 *
 * @return
 */
public function suggestQueries()
{
    $app = app();
    $q = $app->request->get('q');

    // Handle aborted connections
    // hopefully
    if (connection_aborted()) {
        return json([$q]);
    }

    if (empty($q)) {
        return json([$q]);
    }

    $locale = get_theme_active_locale();

    $url = "https://suggestqueries.google.com/complete/search?client=firefox&q={$q}&hl=$locale";
    $http = Http::getSession();
    try {
        $response = $http->get($url);
    } catch (Exception $e) {
        // ssh!
        return json([$q]);
    }

    if (!$response->success) {
        return json([$q]);
    }

    $json = json_decode($response->body, true);

    if (empty($json[1])) {
        return json([$q]);
    }

    $data = [];
    //unset($json[1][0]);

    foreach ($json[1] as $value) {
        $data[] = $value;
    }

    return json($data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...