@IgorPlays

Почему не работает curl в цикле?

Хотел написать код чтобы через цикл выводил в таблицу google данные с AmoCRM.
Вот что вышло,>

<?php

require_once 'access.php';

$webhook_data = file_get_contents('php://input');
$data = json_decode($webhook_data, true);
$leadsId = $_POST['leads']['status'][0]['id'];


$name = '';
$sku = '';
$date = '';
$fullname='';
$phone='';
$comm='';
$premium ='';

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://xxx.amocrm.ru/api/v4/leads/'.$leadsId."?with=contacts",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer '.$access_token
  ),
));

$response = curl_exec($curl);

$reponse_lead = json_decode($response,true);

file_put_contents('tesst.txt', print_r($reponse_lead,true));


$name = $reponse_lead['name'];
for ($i=0; $i < count($reponse_lead['custom_fields_values']) ; $i++) { 
    if($reponse_lead['custom_fields_values'][$i]['field_name'] == 'Направление тура'){
        $sku = $reponse_lead['custom_fields_values'][$i]['values'][0]['value'];
    }
    if($reponse_lead['custom_fields_values'][$i]['field_name'] == 'Дата тура'){
        $date = $reponse_lead['custom_fields_values'][$i]['values'][0]['value'];
    }
}




for ($i=0 ; $i < 10 ; $i++ ) { 
    $contact_id = $reponse_lead['_embedded']['contacts'][$i]['id'];
    if($contact_id == null){
        break;
    }
    $curls = curl_init();

    curl_setopt_array($curls, array(
    CURLOPT_URL => 'https://xxx.amocrm.ru/api/v4/contacts/'.$contact_id,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer '.$access_token
    ),
    ));

    $response_c = curl_exec($curls);

    $response_cont = json_decode($response_c,true);
    $COUNT = count($response_cont['custom_fields_values']);
    $fullname = $response_cont['name'];
    for ($i=0; $i < $COUNT ; $i++) { 
        if($response_cont['custom_fields_values'][$i]['field_code'] == 'PHONE'){
            $phone = $response_cont['custom_fields_values'][$i]['values'][0]['value'];
        }
        if($response_cont['custom_fields_values'][$i]['field_name'] == 'Комментарий'){
            $comm = $response_cont['custom_fields_values'][$i]['values'][0]['value'];
        }
        if($response_cont['custom_fields_values'][$i]['field_id'] == 1120027){
            $premium = $response_cont['custom_fields_values'][$i]['values'][0]['value'];
        }
    }

    if($premium == null || $premium == ''){
        $premium = 0;
    }
    
    $curla = curl_init();
    $url = 'https://script.google.com/macros/s/xxxxxx/exec?gid=1774108540&ID='.$leadsId.'&ID_lid='.$name.'&Article='.$sku.'&date_start='.$date.'&Full_name='.$fullname.'&Phone='.$phone.'&Premium='.$premium.'&Comment='.$comm;
    $url = preg_replace_callback(
        '/[а-яА-Я\s]+/ui',
        static function ($matches) {
            return rawurlencode($matches[0]);
        },
        $url
    );
    curl_setopt_array($curla, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_TIMEOUT => 10,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $responsea = curl_exec($curla);

    

}


Но почему-то он выполняет цикл 1 раз, хотя $coctact_id имеет значение до $i < 5
  • Вопрос задан
  • 95 просмотров
Решения вопроса 1
@Vitsliputsli
У вас и во внешнем и во внутреннем цикле используется одна и та же переменная.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы