I have a for loop which loops through an array of results from a MySQL query.
The query selects all rows from the table wait_times of which the logged in user has created, and is as follows:
SELECT * FROM `wait_times` WHERE `user_id` = ?
My problem is that I need to retrieve the venue information for each row. At the moment, I am looping through that array and sending an individual request to foursquare for the venue’s information. This method can be incredibly slow depending on the amount of rows that have been returned from the query.
In order to speed this up, I plan on storing the venue information in a separate table, which should ultimately be quicker than sending a curl request to the Foursquare API.
Please could you tell me if there are any other ways that could speed this process up?
I looked into multi queries, but the user may not have an account with Foursquare and the API requires the user to be signed up and logged in.
Here is my code:
$query = $this->db->query("SELECT * FROM `wait_times` WHERE `user_id` = ?", array($uid));
$wait_times = $query->result();
foreach ($wait_times as $wait_time) {
$wait_time->venue = $this->venue_info($wait_time->venue_fq_id);
}
function venue_info($vid) {
$v = date('Ymd');
$url = "https://api.foursquare.com/v2/venues/{$vid}?client_id=" . FQ_CLIENT_ID . "&client_secret=" . FQ_CLIENT_SECRET . "&v={$v}";
curl_setopt($this->ch, CURLOPT_URL, $url);
$resp = curl_exec($this->ch) or die(curl_error($this->ch));
$json = json_decode($resp, TRUE);
if ($json['meta']['code'] == 400)
return FALSE;
$venue = $json['response']['venue'];
return $venue;
}
You could do some form of caching? I would highly recommend switch to a reference table instead. FS might have some rule on API requests and you could end up being blacklisted. Any form of HTTPS communication is slow due to the multiple handshakes. If you can, invest time into a table. You could even cheat and use serialized objects.