반복문을 돌릴때 특정 코드를 받으면 계속 돌아가게 하고 싶습니다.

반복문을 돌릴때 특정 코드를 받으면 계속 돌아가게 하고 싶습니다.

QA

반복문을 돌릴때 특정 코드를 받으면 계속 돌아가게 하고 싶습니다.

답변 1

본문

안녕하세요.

curl로 api를 호출을 반복문을 통해 여러번 돌리는 작업을 해야하는데요

한 번 호출시 api에서 rcode = 200이라는 값을 받아오는데

이 값을 받아야 다음 반복이 진행되도록 조건을 주려면 어떻게 해야되나요?

 


<?php
    include_once('./_common.php');
    $sql = "select * from test11";
    $rst = sql_query($sql);
 
    if($decoded['rcode']=="200"){
        while($row = sql_fetch_array($rst)){
            $sct_addr = "TEST";
            $sender = "master";
            $recipient = $row['mb_id'];
            $amount = $row['Amount'];
            $curl = curl_init();
            curl_setopt_array($curl, array(
              CURLOPT_URL => "http://abc.com",
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => "",
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => "POST",
              CURLOPT_POSTFIELDS =>"",
              CURLOPT_HTTPHEADER => array(
                "Content-Type: application/json"
              ),
            ));
            $response = curl_exec($curl);
            curl_close($curl);
            $decoded = json_decode($response, true);
            //print_r($decoded);
            echo $decoded['rcode'];
        }
    }else{
        echo "error";
        exit;
    }
?>

 

위와 같이 소스를 만들었는데요 if($decoded['rcode']=="200"){ 이렇게 조건을 주니 

맨 처음 시작할때가 문제더라구요;;;;;;

이 질문에 댓글 쓰기 :

답변 1


<?php
    include_once('./_common.php');
    $sql = "select * from test11";
    $rst = sql_query($sql);
 
    while($row = sql_fetch_array($rst)){ // 이렇게 반복이고
        $sct_addr = "TEST";
        $sender = "master";
        $recipient = $row['mb_id'];
        $amount = $row['Amount'];
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "http://abc.com",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS =>"",
          CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json"
          ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $decoded = json_decode($response, true);
        //print_r($decoded);
        echo $decoded['rcode'];
        // 건별로 rcode값 확인해서
        if($decoded['rcode']=='200'){
            // 원하는 동작
        } else {
            // 자. 200이 아니면, 무엇을 하길 원하나요? 200 떨어질 때까지 재시도?
        }
    }
답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 119
© SIRSOFT
현재 페이지 제일 처음으로