How to make sure the Curl post has arrived on the other server?

Asked

Viewed 1,774 times

0

How can I be sure that one post reached your destination?

And how can I build the code so that it has no problem with servers when sending the post?

php.

$id = "10";

$nome = "Nome";

$fone = "(99) 9999-9999";

//variável Array responsável por agrupar os dados a serem enviados
$dados = array("id"=>$id,"nome"=>$nome,"fone"=>$fone);       

//URL para onde vai ser enviado nosso POST
$url = "http://answall.com";    

// Aqui inicio a função CURL

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_HEADER, 0);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $dados);

curl_exec($curl);

curl_close($curl);

recebepost.php

$recebeID = $_POST['id'];

$recebeNome = $_POST['nome'];

$recebeFone = $_POST['fone'];

//exibindo as variáveis recebidas através do POST externo

echo "ID: ".$recebeID." - Nome: ".$recebeNome." - Fone: ".$recebeFone;
  • Just make a curl response that works in the same way only in the opposite direction.

  • vc is talking about putting a Curl on the "receive.php" page so that it sends an automatic reply as soon as the post arrives?

  • Yes is one of the options.

  • But when Curl arrives on the other server it gets a response, if that answer came back then it came for sure

  • Got it. My boss wants a system equal to the automatic return of Paypal and Paypal. But I see in the forum of these companies the people complaining that the post has not arrived, There arises a big doubt. I may encounter some problem to send the post to some server or these reported errors are caused by the users themselves who put the post variable on one page and register another?

1 answer

0


I will leave a function below where you have some explanations and I believe it will help you with that question. It is not a ready-to-use function (copy, paste and exit using for all cases), since you will need to modify it according to your needs obviously, but already serves as a good basis to better understand how to detect if the request POST was done correctly and the output is valid.

I usually prefer that the output is on JSON and I usually set whenever the request has a name/key parameter "status" where its default value is SUCCESS and any other value is set to fail, and also use a parameter/name key "msg" where the type of error is reported (for error cases): authentication failure, incomplete data, data in unknown and/or invalid format, etc...

<?php

/**
 * Passa a URL e os parâmetros pela função
 * Para retorno do corpo de resposta, use o parâmetro de referência $body
 * Para retorno de possível erro, use o parâmetro de referência $errormsg
 * A função retorna true em caso de sucesso e false em caso de falha
 * A falha ocorre se as funções do CURL retornarem valores que signifiquem falha
 * A falha também ocorre de acordo com as suas necessidades na validação da resposta
 * Por exemplo (Aplique de acordo com as suas necessidades):
 * 
 * Se a saída for vazia
 * Se a saída tiver um padrão e através de expressão regular retornar falso
 * Se a saída estiver em JSON e obrigatoriamente precisa que exista um parâmetro "status"
 * Se a saída estiver sm JSON e o parâmetro "status" seja diferente de "SUCCESS"
 * 
 * A função abaixo é apenas uma demonstração de como saber se algo deu certo ou não
 * Modifique de acordo com as suas necessidades
 */

function curl_post_42252( $url, $params = array(), &$body = null, &$errormsg = null ) {

    $fields = http_build_query( $params, '', '&' );

    set_time_limit(60);

    $ch = curl_init();

    $options = array(
        CURLOPT_CONNECTTIMEOUT => 30,
        CURLOPT_ENCODING       => '',
        CURLOPT_FOLLOWLOCATION => false,
        CURLOPT_HEADER         => true,
        CURLOPT_NOPROGRESS     => false,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $fields,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_TIMEOUT        => 60,
        CURLOPT_URL            => $url,
        CURLOPT_USERAGENT      => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:32.0) Gecko/20100101 Firefox/32.0',
        CURLOPT_VERBOSE        => true,
    );

    curl_setopt_array( $ch, $options );

    $exec  = curl_exec( $ch );
    $info  = curl_getinfo( $ch );
    $head  = substr( $exec, 0, $info['header_size'] );
    $body  = substr( $exec, $info['header_size'] );
    $error = curl_error( $ch );
    $errno = curl_errno( $ch );

    curl_close( $ch );

    /**
     * Início das verificações para saber se a requisição foi feita corretamente
     */

    /**
     * Exec retornou falso?
     */

    if ( $exec === false ) {
        $errormsg = 'curl_exec';
        return false;
    }

    /**
     * Tem algum erro?
     */

    if ( $error !== '' ) {
        $errormsg = 'curl_error';
        return false;
    }

    /**
     * Tem algum erro? (Redundante)
     */

    if ( $errno ) {
        $errormsg = 'curl_errno';
        return false;
    }

    /**
     * A parte acima verifica se não houve erros nas funções do CURL (HTTPS, Timeout, etc...)
     * Daqui para baixo estão os exemplos de como validar e saber se realmente a resposta de saída é válida
     * Usando expressão regular ou JSON
     * Adapte de acordo com as suas necessidades
     */

    return true;

    /**
     * Código HTTP de resposta é diferente de 200? Geralmente o código HTTP de resposta é 200
     */

    if ( $info['http_code'] !== 200 ) {
        $errormsg = 'curl_http_code';
        return false;
    }

    /**
     * Corpo (resposta) (html/json/xml) é vazio? Geralmente há uma saída em JSON ou com algum valor simples informando falha ou sucesso
     */

    if ( trim( $body ) === '' ) {
        $errormsg = 'curl_body_empty';
        return false;
    }

    /**
     * Validação por expressão regular (Exemplo fictício)
     */

    $pattern = '/ID\:/';

    if ( !preg_match( $pattern, $body ) ) {
        $errormsg = 'pattern_dont_match';
        return false;
    }

    /**
     * Validação por valor JSON, supondo que a resposta esteja formatada em JSON e possua a chave "status" com o valor "SUCCESS" ou "ERROR" para Sucesso ou Erro
     */

    if ( ( $json = json_decode( $body ) ) === false || $json === null ) {
        $errormsg = 'json_decode_error';
        return false;
    }

    if ( !isset( $json->status ) ) {
        $errormsg = 'json_status_undefined';
        return false;
    }

    if ( $json->status !== 'SUCCESS' ) {
        $errormsg = 'json_status_fail';
        return false;
    }

    return true;

}

$url = 'http://answall.com';

$parametros = array(
    'id'   => 'valor_id',
    'nome' => 'valor_nome',
    'fone' => 'valor_fone',
);

if ( !curl_post_42252( $url, $parametros, $resposta, $erro ) ) {
    echo $erro;
    die();
}

echo 'Successo<br/><br/>';
echo '<pre>';
var_dump( $erro );
var_dump( $resposta );
echo '</pre>';
die();

?>
  • Thank you very much friend.

Browser other questions tagged

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