Fix slow external (third-party) connections in CS-Cart¶
Impact¶
External connections(to third-party services) without timeouts can break down your website in case of third-party service unavailability. We highly recommend don’t use curl_init
/file_get_contents
/etc without timeouts. If you using CS-Cart, we recommend use TyghHttp for the connections to external services and to follow the CS-Cart coding standards.
Poorly written code without timeouts can break down:
- Database. In case of using all mysql workers, your queue of queries will grow,but users will not be able the access a website. This is most often a case, which leads the website unavailability.
- PHP. In case of using all PHP workers, your queue of requests will grow, but users will don't be able to access a website. In most cases, it will lead to Database error.
HowTo: The Correct Way¶
For example, this code example can help you fix your source code for CS-Cart coding standarts.
<?php
use Tygh\Http;
///...
$url = 'https://some-api.scalesta.com';
$extra = ['headers' => ['timeout' => 3],
'basic_auth' => [$login,$password]];
$data = ['param' => 'value'];
$response = Http::get($url, $data, $extra);
$response = json_decode($response, true);
//...
Because of we are not the simple hosting service, and we make a lot of investigations and researches. We have detected a lot of issues in CS-Cart and different addons. And we provide ways for improving speed and stability of websites and CS-Cart.
Bad Examples¶
curl_*¶
<?php
//...
} elseif (!empty($new_image_data_array['url'])) {
$ch = curl_init($new_image_data_array['url']);
$fp = fopen($image_data['absolute_path'], 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$optimized = 'Y';
} else {
//...
file_get_contents¶
Using the file_get_contents
this is solution from the 1990. This is very bag and incorrect way!
Hint
If you have a problem, need assistance with tweaks or a free consultation, if you just want to discuss your project with experts and estimate the outcome, if you're looking for a solution that reinforces your online business, we will help. Let us know through Scalesta account or email.