K
K
kolomat2021-06-25 18:00:27
PHP
kolomat, 2021-06-25 18:00:27

Deal with multiCurl?

Good evening, I’m sorry right away, I couldn’t describe the essence of the issue in the title of the normal. The problem in general is that, there is an API on the endpoint, the id of the product is transmitted, in response it will receive a description, prices, etc., but since there are a lot of products, I decided to try mCurl, but then I ran into such a problem. The code itself

$perRequest = 200;

for ($i = 0; $i < count($products); $i += $perRequest) {
    $currentNodes = array_slice($products, $i, $perRequest);

    $mh = curl_multi_init();

    foreach ($currentNodes as $key => $value) {
        $multiCurl[$key] = curl_init();
        curl_setopt_array($multiCurl[$key], array(
            CURLOPT_URL => 'https://site.com/' . $value['sku'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array(
                "Authorization: Bearer " . $token,
            ),
        ));
        curl_multi_add_handle($mh, $multiCurl[$key]);
    }

    $index = null;

    do {
        curl_multi_exec($mh,$index);
    } while ($index > 0);

    foreach ($multiCurl as $k => $ch) {
        $data[$k][$i] = json_decode(curl_multi_getcontent($ch), TRUE);
        curl_multi_remove_handle($mh, $ch);
    }
}

The $data array contains all the necessary data for updating, but the problem is that there is no identifier by which you can identify and update the product. That is, the question is actually what, help me understand whether it is possible to transfer it to the $data array somehow, or with this approach it is impossible. Thanks in advance for ideas and tips. $value['sku']

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question