V
V
Vladimir Kulikov2020-05-16 16:28:39
PHP
Vladimir Kulikov, 2020-05-16 16:28:39

How can you optimize php code?

Hello, how can I optimize this code? As I understand it, the problem is in this line: $api_response = wp_remote_post($url_api , $params);

Mistake:

Fatal error: Maximum execution time of 30 seconds exceeded in A:\XAMP\htdocs\test\wp-includes\Requests\Transport\cURL.php on line 441

The code:
$arr_tovar = $result->products;
$url_api = $url_site.'/wp-json/wc/v3/products';
$params = [
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( 'admin:123123123' )
    ),
    'body' => [],
];
foreach ($arr_tovar as $item) {
     $params['body'] = array(
        'name' => $item->name, 
        'status' => 'pending', 
        'id_iiko_rovara' => $item->productCategoryId,
        'categories' => array(
            array( 
                'id_iiko_категории' => $item->productCategoryId 
            )
        ),
        'price' => $item->price
    );

    $api_response = wp_remote_post($url_api , $params);

    $body = json_decode( $api_response['body'] );
    if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
        echo 'Этот продукт ' . $body->name . ' успешно создан <br>';
    }else{
        echo 'Ошибка';
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Shamanov, 2020-05-16
@SilenceOfWinter

no way, you need to increase the script execution time (execution_time) to 60 seconds in php.ini

F
FanatPHP, 2020-05-16
@FanatPHP


Query this data by cron and save it to a local file

P
Pychev Anatoly, 2020-05-16
@pton

There is such a thing as a queue of background processes. You can see here .
Woo, by the way, sits on it, i.e. among its files is this library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question