简体   繁体   中英

AJAX-Request combined with cURL and PHP takes too long to load

I am working on my local machine with xampp and every other ajax request works very well and fast. My current application takes 21 seconds to load 3.5 kilobyte of data.

This is a bit slow if you ask me. So what is my software doing?

The software has 4 radio buttons. The user chooses one and then an ajax-request is created

var radio = $('input[name="selection"]:checked').val();
var hidden = $('#hiddenFive').val();
$.ajax
({  
    type: 'GET',
    url: 'curl.php',
    data: {type: radio, region: 'Hampshire', hidden: hidden},
    success: function(response, textStatus, XMLHttpRequest) 
    {
        var obj = jQuery.parseJSON(response);
        var strin = '';
        for(var i = 0; i < obj.length; i++)
        {
            strin += "<b>Name of " + radio + ": </b>" + obj[i].name + "<b> | Region: </b>" + obj[i].region + "<br>";    
        }
        $('#result').html(strin);
    },
    error: function(response, status, error)
    {
        alert("Error"); 
    }
}); 

The data gets sent to the site curl.php which sends a curl request to an external web site. I wrote external cursive, because it is on the same folder, we should just act like it is external and we need data with cURL from it (Homework you know)

So, now, the URL looks like this

http://localhost/htmlAssignment/5/curl.php?type=town&region=Hampshire&hidden=hiddenFive

And the curl.php saves those parameters in variables and then sends it to the web service.php

<?php
$type = $_GET['type'];
$region = $_GET['region'];
$hidden = $_GET['hidden'];
$url = "http://localhost/htmlAssignment/webservice.php?type=" . $type . "&region=" .$region. "&hidden=".$hidden;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($ch);
curl_close($ch); 
echo $output;
?>

The webservice.php

checks if the hidden-value is hiddenFive, what is correct and then calls a db-function

if(isset($_GET['type']) && isset($_GET['region'])  && $_GET['hidden'] == 'hiddenFive')
{
    $array = array();
    $region = $_GET['region'];
    $type = $_GET['type'];
    $array = $db->getMultiDataPOI($type, $region);
    echo json_encode($array);           
}

My db function looks like this

function getMultiDataPOI($type, $region)
    {
        $getMultiSQL = "SELECT ID, name, type, country, region, lon, lat, description FROM pointsofinterest WHERE type = :type AND region = :region";
        $getMultiPrepare = $this->prepare($getMultiSQL);
        $getMultiPrepare->bindParam(':type', $type);
        $getMultiPrepare->bindParam(':region', $region);
        $getMultiPrepare->execute();
        $getMultiResult = $getMultiPrepare->fetchAll();
        return $getMultiResult;
    }

After those 20 seconds the data gets displayed normally and as it should, but it takes 20 seconds, what is ab it odd if you ask me. What could be the problem here for that long time? As I said I'm working on localhost with xampp and any other AJAX-request which uses the same webservice loads instantly. Why does this take so long?

Here is a screen if it helps from FireBug. I hope you can read it.

请求的FireBug屏幕

Just from my experience, I've found localhost to often cause strange behaviour, such as what you're describing. Have you simply tried changing your reference of localhost to 127.0.0.1 and use that? Localhost has to still do a name lookup, which can sometimes be unreliable from your hosts file, see What is the difference between 127.0.0.1 and localhost

Also I would try to open the PHP service file directly, the one you're calling in your AJAX, to make sure it loads fast by itself. Since it's doing an SQL query, there could be something with your query or even your MySQL configuration that's causing you some lag.

On another note, I'd use caution with your webserver.php file. All your request data is not being filtered and going directly into your DB which leaves your application open to SQL injections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM