Alternative connection to Webservice

Asked

Viewed 37 times

2

good afternoon!

I have the following function:

function API($conteudoAEnviar) {
    try{
        $cabecalho = array(
                'Content-Type: application/json',
                'Authorization: Basic ' . base64_encode(TOTVS_JSON_USER_SECRET . ':' . TOTVS_JSON_PASSWORD_SECRET)
            );

        $ch = curl_init(TOTVS_URL_REST);

            $tpRequisicao = 'POST';

        if ($tpRequisicao == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);

            curl_setopt($ch, CURLOPT_POSTFIELDS, $conteudoAEnviar);
        }

        if (!empty($cabecalho)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $cabecalho);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $resposta = curl_exec($ch);

        curl_close($ch);
    }catch(Exception $e){
        return $e->getMessage();
    }
    return $resposta;
}

Therefore, I need to create a way for when the $reply presents some error or failure, whether connection or anything, that I can change to a local connection to the database, could give me a light on this, I found nothing in the searches. Is there any function that checks if the connection has been successful?

  • The function curl_exec returns false in case of failure, with this you can use curl_error and curl_errno to check what happened.

  • 1

    Marcos, please do not put SOLVED in the title, this is not quite how the site works. If you found the solution, post it as a response in the field below and mark as accepted. Visit our [tour] to learn how it works.

  • 1

    Marcos, in a question and answer system, such as [pt.so], there is a clear difference between question and answer: one is question, another is answer. That said, I advise you to publish at all times the solution as answer, not editing the question.

1 answer

0

Does the check with curl_error($ch), in the event of failure, make an exception throw new Exception($error_msg, 1), treats the exception by trying a new request: it would look something like:

function API($conteudoAEnviar) {
        try {
            $cabecalho = array(
                'Content-Type: application/json',
                'Authorization: Basic ' . base64_encode(TOTVS_JSON_USER_SECRET . ':' . TOTVS_JSON_PASSWORD_SECRET)
            );

            $ch = curl_init(TOTVS_URL_REST);
            $tpRequisicao = 'POST';

            if ($tpRequisicao == 'POST') {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $conteudoAEnviar);
            }

            if (!empty($cabecalho)) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $cabecalho);
            }

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $resposta = curl_exec($ch);

            if (curl_error($ch)) {
                $error_msg = curl_error($ch);
                throw new Exception($error_msg, 1);
            }

            curl_close($ch);

        } catch (Exception $e) {

            try {
                echo "Inplementar aqui a outra chamada";
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.