PHP code example of solutosoft / php-receita

1. Go to this page and download the library: Download solutosoft/php-receita library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

solutosoft / php-receita example snippets



use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Solutosoft\Receita\Client;

// Retorna a imagem do captcha e os cookies são enviados juntamente através do header `X-CNPJ-Cookies`
$app->get('/cnpj-captcha', function (Request $request, Response $response) {
    $client = new Client();
    $captcha = $client->getCaptcha();      
    
    return $response
        ->withHeader('X-CNPJ-Cookies', http_build_query($client->getCookies())
        ->withHeader('Content-Length', mb_strlen($captcha, '8bit')); 
        ->withBody($captcha);
});

// Retorna as informações do CNPJ, porém deve ser enviado os cookies da requisição anterior e o texto do captcha.
// Exemplo: 
// http://localhost/18771001000103/abc123?cookies[ASPSESSIONIDAUBSSQDS]=OEAEGIOBFOGKOLALLCFEEDIL&cookies[sto-id-47873]=FHOHJEKBLLAB

$app->get('/cnpj-info/{cnpj}/{captcha}', function (Request $request, Response $response, array $args) {
    $cookies = $request->getQueryParam('cookies'),

    parse_str($args['cookies'], $result);
    $client = new Client(['timeout' => 5]);
    $info = $client->findByCNPJ($args['cnpj'], $args['captcha'], $result);
    
    return $response
        ->withHeader('Content-Type', 'application/json')        
        ->write(json_encode($info));
});

$app->run();

php composer.phar 

"solutosoft/php-receita": "*"