GuzzleHttp

This commit is contained in:
Michael RICOIS 2016-12-15 14:37:22 +01:00
parent 2dc866f460
commit 94147c5f19
3 changed files with 288 additions and 94 deletions

View File

@ -6,7 +6,8 @@
"geshi/geshi": "dev-master",
"league/csv": "^8.1",
"doctrine/dbal": "^2.5",
"mikehaertl/phpwkhtmltopdf": "^2.2"
"mikehaertl/phpwkhtmltopdf": "^2.2",
"guzzlehttp/guzzle": "^6.2"
},
"include-path": ["library/"],
"autoload": {

225
composer.lock generated
View File

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "e99c2a7874dfdf3765c2593f120bbfb2",
"content-hash": "0d51a8356b2f1dd421c52d81f2d1331b",
"hash": "052609e0ef5c4c189a93c0197af46986",
"content-hash": "f1e440444eb2e2707d495c95551a35f6",
"packages": [
{
"name": "doctrine/annotations",
@ -513,6 +513,177 @@
"homepage": "http://qbnz.com/highlighter/",
"time": "2016-02-01 23:14:34"
},
{
"name": "guzzlehttp/guzzle",
"version": "6.2.2",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "ebf29dee597f02f09f4d5bbecc68230ea9b08f60"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/ebf29dee597f02f09f4d5bbecc68230ea9b08f60",
"reference": "ebf29dee597f02f09f4d5bbecc68230ea9b08f60",
"shasum": ""
},
"require": {
"guzzlehttp/promises": "^1.0",
"guzzlehttp/psr7": "^1.3.1",
"php": ">=5.5"
},
"require-dev": {
"ext-curl": "*",
"phpunit/phpunit": "^4.0",
"psr/log": "^1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "6.2-dev"
}
},
"autoload": {
"files": [
"src/functions_include.php"
],
"psr-4": {
"GuzzleHttp\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle is a PHP HTTP client library",
"homepage": "http://guzzlephp.org/",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"rest",
"web service"
],
"time": "2016-10-08 15:01:37"
},
{
"name": "guzzlehttp/promises",
"version": "1.3.0",
"source": {
"type": "git",
"url": "https://github.com/guzzle/promises.git",
"reference": "2693c101803ca78b27972d84081d027fca790a1e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/2693c101803ca78b27972d84081d027fca790a1e",
"reference": "2693c101803ca78b27972d84081d027fca790a1e",
"shasum": ""
},
"require": {
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle promises library",
"keywords": [
"promise"
],
"time": "2016-11-18 17:47:58"
},
{
"name": "guzzlehttp/psr7",
"version": "1.3.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "5c6447c9df362e8f8093bda8f5d8873fe5c7f65b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/5c6447c9df362e8f8093bda8f5d8873fe5c7f65b",
"reference": "5c6447c9df362e8f8093bda8f5d8873fe5c7f65b",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"psr/http-message": "~1.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "PSR-7 message implementation",
"keywords": [
"http",
"message",
"stream",
"uri"
],
"time": "2016-06-24 23:00:38"
},
{
"name": "league/csv",
"version": "8.1.1",
@ -691,6 +862,56 @@
],
"time": "2016-06-02 08:06:45"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06 14:39:51"
},
{
"name": "zendframework/zendframework1",
"version": "1.12.20",

View File

@ -1,5 +1,6 @@
<?php
require_once 'framework/common/curl.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class Metier_Partenaires_MTva
{
@ -141,103 +142,74 @@ class Metier_Partenaires_MTva
*/
protected function getRemoteVAT()
{
$nbIndispo = 0;
while (true) {
$postData = array(
'memberStateCode' => "FR",
'number' => $this->cle.$this->siren,
'traderName' => "",
'traderStreet' => "",
'traderPostalCode' => "",
'traderCity' => "",
'requesterMemberStateCode' => "",
'requesterNumber' => "",
'action' => "check",
'check' => "Vérifier",
);
$tdeb = microtime(true);
$url = 'http://ec.europa.eu/taxation_customs/vies/vatRequest.html';
$referer = 'http://ec.europa.eu/taxation_customs/vies/';
$page = getUrl($url, '', $postData, $referer, false, 'ec.europa.eu', '', 15);
$duree = round(microtime(true)-$tdeb, 3);
if ($page['err_num'] != 0) {
Metier_Util_Log::write('W', 'TVA Erreur CURL n°'. $page['err_num'] .', '. $page['err_msg'] ." sur le Siren $this->siren, numéro de TVA = FR $cle $this->siren. Durée = $duree s !", __LINE__, __FILE__, __FUNCTION__, __CLASS__);
$this->vatNumber = "FR".$this->cle.$this->siren;
$this->vatDefined = false;
$url = 'http://ec.europa.eu/taxation_customs/vies/vatResponse.html';
return true;
}
$client = new Client();
try {
$timeStart = microtime();
$response = $client->request('POST', $url, [
'form_params' => [
'memberStateCode' => "FR",
'number' => $this->cle.$this->siren,
'traderName' => "",
'traderStreet' => "",
'traderPostalCode' => "",
'traderCity' => "",
'requesterMemberStateCode' => "",
'requesterNumber' => "",
'action' => "check",
'check' => "Vérifier",
],
]);
$timeStop = microtime();
$time = $timeStop - $timeStart;
$code = $page['code'];// Code réponse Serveur
$body = $page['body'];
$header = $page['header'];
if (preg_match('/Yes, valid VAT number/i', $body) || preg_match('/Oui, numéro de TVA valide/i', $body)) {
$tabInsert = array(
'siren' => $this->siren,
'cle' => $this->cle,
'duree' => $duree
);
if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) {
$tabUpdate = array(
'cle' => $this->cle,
'duree' => $duree
);
if (!$this->iDb->update('sdv1.siren_tva', $tabUpdate, "siren=$this->siren")) {
Metier_Util_Log::write('W', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren, impossible de MAJ la clef ($duree s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__);
}
}
Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $$this->cle $siren ($duree s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__);
$this->vatNumber = "FR".$this->cle.$this->siren;
$this->vatDefined = true;
return true;
} elseif (strpos($body, 'Service non disponible') > 0 || strpos($body, 'ponse trop long.') > 0) {
$nbIndispo++;
if ($nbIndispo < 3) {
Metier_Util_Log::write('I', "TVA Siren $this->siren, Service de l'état membre indisponible. Mise en sommeil...", __LINE__, __FILE__, __FUNCTION__, __CLASS__);
sleep(rand(1, 2));
$nbIndispo = 0;
} else {
$tabInsert = array(
'siren' => $this->siren,
'cle' => 'NULL',
'duree' => $duree
);
$tabUpdate = array(
'cle' => 'NULL',
'duree' => $duree
);
if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) {
if (!$this->iDb->update('sdv1.siren_tva', $tabUpdate, "siren=$this->siren")) {
Metier_Util_Log::write('W', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren, impossible de MAJ la clef ($duree s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__);
if ($response->getStatusCode() == 200) {
$body = $response->getBody();
if (preg_match('/Yes, valid VAT number/i', $body)
|| preg_match('/Oui, numéro de TVA valide/i', $body)) {
$tabInsert = array(
'siren' => $this->siren,
'cle' => $this->cle,
'duree' => $time
);
if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) {
$tabUpdate = array(
'cle' => $this->cle,
'duree' => $time
);
if (!$this->iDb->update('sdv1.siren_tva', $tabUpdate, "siren=$this->siren")) {
Metier_Util_Log::write('W', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren, impossible de MAJ la clef ($time s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__);
}
}
Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $this->cle $siren ($time s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__);
$this->vatNumber = "FR".$this->cle.$this->siren;
$this->vatDefined = true;
return true;
}
Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren vérification non disponible ($duree s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__);
$this->vatNumber = "FR".$this->cle.$this->siren;
$this->vatDefined = false;
return true;
}
} else {
$tabInsert = array(
'siren' => $this->siren,
'cle' => 'NULL',
'duree' => $duree
);
if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) {
$tabUpdate = array(
'cle' => 'NULL',
'duree' => $duree
);
if (!$this->iDb->update('sdv1.siren_tva', $tabUpdate, "siren=$this->siren")) {
Metier_Util_Log::write('W', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren, impossible de MAJ la clef ($duree s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__);
}
}
Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren non attribué ($duree s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__);
$this->vatNumber = "FR".$this->cle.$this->siren;
$this->vatDefined = false;
}
} catch (RequestException $e) {
Metier_Util_Log::write('I', "TVA ".$e->getMessage(), __LINE__, __FILE__, __FUNCTION__, __CLASS__);
}
return true;
$tabInsert = array(
'siren' => $this->siren,
'cle' => 'NULL',
'duree' => $time
);
$tabUpdate = array(
'cle' => 'NULL',
'duree' => $time
);
if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) {
if (!$this->iDb->update('sdv1.siren_tva', $tabUpdate, "siren=$this->siren")) {
Metier_Util_Log::write('W', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren, impossible de MAJ la clef ($time s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__);
}
}
Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren vérification non disponible ($time s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__);
$this->vatNumber = "FR".$this->cle.$this->siren;
$this->vatDefined = false;
return true;
}
/**