<?php

header('Content-Type: text/html; charset=utf-8');

echo "<h1>Teste de chamadas REST - Millennium</h1>";
echo "<p>Para a lista completa de chamadas, consulte: <a href='http://millennium.iwise.com.br:888/api/millenium_eco/\$help'> http://millennium.iwise.com.br:888/api/millenium_eco/\$help</a></p>";

echo "<h3>Teste de consulta de produtos usando GET</h3>";
echo "<h4>millenium_eco/produtos/listavitrine</h4>";

$curl = curl_init("http://millennium.iwise.com.br:888/api/millenium_eco/produtos/listavitrine?vitrine=9&\$top=1");
try{
$options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page        
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings        
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects        
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_SSL_VERIFYHOST => FALSE,
        CURLOPT_USERPWD        => "odata:0d@t@123",
        CURLOPT_HTTPAUTH       => CURLAUTH_NTLM,        
        CURLOPT_HTTPHEADER     => array('accept: application/json','accept-encoding: deflate')
    );

curl_setopt_array($curl,$options);

if( ! $response = curl_exec($curl)) 
    { 
        trigger_error(curl_error($curl)); 
    } 

$json_response = json_decode($response);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
}
finally{
curl_close($curl);
};

echo "<h4>Status:".$status."</h4>";
echo "<h4>Dump do retorno:</h4>";

echo var_dump($json_response);

echo "<h3>Teste de gravação de cliente usando POST</h3>";
/*
** EXEMPLO USANDO POST **
vamos montar este json usando um objeto literal PHP:
{"cpf":"118.299.498-96",
 "rg":"21.559.517",
 "nome":"Cliente de Testes",
 "data_aniversario":"1970-1-1", //formato de data yyyy-mm-ddThh:mm:ss
 "endereco":[{"logradouro":"Rua de Testes",
              "numero":31,
              "complemento":"Apto 52",
              "cep":"02720-140",
              "cidade":"SAO PAULO",
              "estado":"SP",
              "ddd":"11",
              "fone":"2332-2100"
 }]
}
//se você for usar PHP anterior ao 5.4, use array() ao inves do literal []
*/

$cliente = (object)["cpf"=>"118.299.498-96",
                    "rg"=>"21.559.517",
                    "nome"=>"Cliente de Testes",
                    "data_aniversario"=>"1970-1-1", //formato de data yyyy-mm-ddThh:mm:ss
                    "endereco"=>[
                       (object)["logradouro"=>"Rua de Testes",
                                "numero"=>31,
                                "complemento"=>"Apto 52",
                                "cep"=>"02720-140",
                                "cidade"=>"SAO PAULO",
                                "estado"=>"SP",
                                "ddd"=>"11",
                                "fone"=>"2332-2100"
                               ]
                 ]
            ];


$json_request = json_encode($cliente);
echo $json_request;

echo "<br><br>";

$options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page        
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings        
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects        
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_SSL_VERIFYHOST => FALSE,
        CURLOPT_USERPWD        => "odata:0d@t@123",
        CURLOPT_HTTPAUTH       => CURLAUTH_NTLM,    
        CURLOPT_POSTFIELDS     => $json_request,
        CURLOPT_HTTPHEADER     => array('content-type: application/json','content-length:' .strlen($json_request)),
        CURLOPT_CUSTOMREQUEST  => "POST"
    );

$curl = curl_init("http://millennium.iwise.com.br:888/api/millenium_eco/clientes/IncluirOuAlterar");
try{
    curl_setopt_array($curl,$options);

    if( ! $response = curl_exec($curl)) 
        { 
            trigger_error(curl_error($curl)); 
        } 

    $json_response = json_decode($response);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
}
finally{
curl_close($curl); //vamos sempre fechar o curl
};

echo "<h4>Status:".$status."</h4>";
echo "<h4>Dump do retorno:</h4>";

echo var_dump($json_response);
?>
