How to: Development: Fix The External (third-party) connections

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:

  1. Database. In case of using all mysql workers, your queue of queries will grow, but users will don’t be able to access a website.
    This is most often a case, which leads to website unavailability.
  2. 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 standards.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use Tygh\Http;
...
$url = 'https://some-api.cloud.simtechdev.us';
$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_*

} 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 MyCloud or email.