Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #32 from giansalex/feature/http1_1-requests
Browse files Browse the repository at this point in the history
HTTP 1.1
  • Loading branch information
giansalex authored Oct 16, 2020
2 parents 0115fe2 + 830212e commit d0b3f84
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 18 deletions.
11 changes: 7 additions & 4 deletions docs/dni.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@
Consulta de DNI.
> Fuente: **JNE**.
## Requerimientos
- Tener activo [allow_url_fopen](https://www.php.net/manual/es/filesystem.configuration.php#ini.allow-url-fopen).

## Ejemplo

```php
use Peru\Http\ContextClient;
use Peru\Jne\{Dni, DniParser};
use Peru\Jne\DniFactory;

require 'vendor/autoload.php';

$dni = '46658592';

$cs = new Dni(new ContextClient(), new DniParser());
$factory = new DniFactory();
$cs = $factory->create();

$person = $cs->get($dni);
if (!$person) {
echo 'Not found';
exit();
return;
}

echo json_encode($person);
Expand Down
11 changes: 6 additions & 5 deletions docs/ruc.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@ Consulta de RUC.
## Requerimientos
- Tener cargada la extension `dom`.
- Tener activo [allow_url_fopen](https://www.php.net/manual/es/filesystem.configuration.php#ini.allow-url-fopen).

## Ejemplo

```php
use Peru\Http\ContextClient;
use Peru\Sunat\{HtmlParser, Ruc, RucParser};
use Peru\Sunat\RucFactory;

require 'vendor/autoload.php';

$ruc = '20100070970';

$cs = new Ruc(new ContextClient(), new RucParser(new HtmlParser()));
$factory = new RucFactory();
$cs = $factory->create();

$company = $cs->get($ruc);
if (!$company) {
echo 'Not found';
exit();
return;
}

echo json_encode($company);

```

!!! tip "Cambiar URL utilizada"
Si necesita cambiar la url utilizada internamente para obtener la información del RUC, puede hacerlo de esta forma:
Si necesita cambiar la url utilizada internamente para obtener la información del RUC, este es un ejemplo:
`$cs->urlConsult='http://e-consultaruc.sunat.gob.pe/cl-ti-itmrconsruc/jcrS03Alias';`

## Resultado
Expand Down
2 changes: 1 addition & 1 deletion docs/user-sol.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $ruc = '20123456789'; // colocar un ruc válido
$user = 'TGGMMSYY'; // colocar un usuario según el ruc

$cs = new UserValidator(new ContextClient());
$valid = $cs->valid($dni);
$valid = $cs->valid($ruc, $user);
if ($valid) {
echo 'Válido';
} else {
Expand Down
14 changes: 9 additions & 5 deletions src/Peru/Http/Async/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
class HttpClient extends Browser implements ClientInterface
{
private const USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.0.1';

/**
* @var array
*/
Expand Down Expand Up @@ -76,12 +78,14 @@ private function saveCookies(array $headers)

private function buildHeaders(array $headers)
{
if (empty($this->cookies)) {
return $headers;
}
$defaultHeaders = [
'User-Agent' => self::USER_AGENT,
];

$headers['Cookie'] = implode('; ', $this->cookies);
if (!empty($this->cookies)) {
$defaultHeaders['Cookie'] = implode('; ', $this->cookies);
}

return $headers;
return array_merge($defaultHeaders, $headers);
}
}
23 changes: 20 additions & 3 deletions src/Peru/Http/ContextClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class ContextClient implements ClientInterface
{
private const FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded';
private const USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.0.1';

private const HTTP_VERSION = 1.1;

/**
* stream_context extra options.
*
Expand Down Expand Up @@ -76,17 +77,19 @@ public function post(string $url, $data, array $headers = [])
*/
private function getContext(string $method, $data, array $headers)
{
$headers['Connection'] = 'close';
$defaultOptions = [
'http' => [
'header' => $this->join(': ', $headers),
'method' => $method,
'content' => $this->getRawData($data),
'user_agent' => self::USER_AGENT,
'protocol_version' => self::HTTP_VERSION,
],
];

if (!empty($this->options)) {
$defaultOptions = array_merge_recursive($defaultOptions, $this->options);
if (!empty($this->options) && is_array($this->options)) {
$defaultOptions = $this->mergeOptions($defaultOptions, $this->options);
}

if (!empty($this->cookies)) {
Expand Down Expand Up @@ -136,4 +139,18 @@ private function getResponseAndSaveCookies(string $url, $ctx)

return $response;
}

private function mergeOptions(array $default, array $overwrite): array
{
$merged = $default;
foreach($overwrite as $key => $value) {
if (array_key_exists($key, $default) && is_array($value)) {
$merged[$key] = $this->mergeOptions($default[$key], $overwrite[$key]);
} else {
$merged[$key] = $value;
}
}

return $merged;
}
}
16 changes: 16 additions & 0 deletions src/Peru/Jne/DniFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Peru\Jne;

use Peru\Http\ContextClient;
use Peru\Services\DniInterface;

class DniFactory
{
public function create(): DniInterface
{
return new Dni(new ContextClient(), new DniParser());
}
}
16 changes: 16 additions & 0 deletions src/Peru/Sunat/RucFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Peru\Sunat;

use Peru\Http\ContextClient;
use Peru\Services\RucInterface;

class RucFactory
{
public function create(): RucInterface
{
return new Ruc(new ContextClient(), new RucParser(new HtmlParser()));
}
}
21 changes: 21 additions & 0 deletions tests/Peru/Jne/DniFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Peru\Jne;

use Peru\Jne\Dni;
use Peru\Jne\DniFactory;
use PHPUnit\Framework\TestCase;

class DniFactoryTest extends TestCase
{
public function testCreate()
{
$factory = new DniFactory();

$cs = $factory->create();

$this->assertInstanceOf(Dni::class, $cs);
}
}
21 changes: 21 additions & 0 deletions tests/Peru/Sunat/RucFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Peru\Sunat;

use Peru\Sunat\Ruc;
use Peru\Sunat\RucFactory;
use PHPUnit\Framework\TestCase;

class RucFactoryTest extends TestCase
{
public function testCreate()
{
$factory = new RucFactory();

$cs = $factory->create();

$this->assertInstanceOf(Ruc::class, $cs);
}
}
3 changes: 3 additions & 0 deletions tests/Peru/Sunat/RucTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function setUp()
{
$client = new ContextClient();
$client->options = [
'http' => [
'protocol_version' => 1.1
],
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
Expand Down

0 comments on commit d0b3f84

Please sign in to comment.