initial commit
This commit is contained in:
commit
b9e3840ebf
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
vendor/
|
||||||
|
composer.lock
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2016-2017 meta-tech.academy
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
99
README.md
Normal file
99
README.md
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
|
||||||
|
# MetaTech PwsClient
|
||||||
|
|
||||||
|
a php webservice client managing [ PwsAuth ](https://github.com/meta-tech/pws-auth) protocol
|
||||||
|
|
||||||
|
### Requirements
|
||||||
|
|
||||||
|
PHP >= 5.4
|
||||||
|
|
||||||
|
### Install
|
||||||
|
|
||||||
|
The package can be installed using [ Composer ](https://getcomposer.org/). (not yet)
|
||||||
|
```
|
||||||
|
composer require meta-tech/pws-client
|
||||||
|
```
|
||||||
|
|
||||||
|
Or add the package to your `composer.json`.
|
||||||
|
|
||||||
|
```
|
||||||
|
"require": {
|
||||||
|
"meta-tech/pws-client" : "1.0"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
When instantiating, PwsClient automatically checks if it has been authenticated. Otherwise, or if the session has expired,
|
||||||
|
the client will perform the authentication. Then, you can initiate get or post call
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
require_once(__dir__ . '/vendor/autoload.php');
|
||||||
|
|
||||||
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
use MetaTech\PwsAuth\Authenticator;
|
||||||
|
use MetaTech\Ws\Client;
|
||||||
|
|
||||||
|
$config = Yaml::parse(file_get_contents(__dir__ . '/config/pwsauth.yml'));
|
||||||
|
$authenticator = new Authenticator($config);
|
||||||
|
$config = Yaml::parse(file_get_contents(__dir__ . '/config/pwsclient.yml'));
|
||||||
|
|
||||||
|
$client = new Client($config, $authenticator);
|
||||||
|
// on instanciation the client init this calls :
|
||||||
|
// $client->check();
|
||||||
|
// $client->call();
|
||||||
|
|
||||||
|
// get example
|
||||||
|
$response = $client->get('/ws/person/222');
|
||||||
|
if ($response->done) {
|
||||||
|
// do stuff
|
||||||
|
|
||||||
|
}
|
||||||
|
// post example
|
||||||
|
$client->post('/ws/person/222/update', [ 'firstname' => 'toto']);
|
||||||
|
if ($response->done) {
|
||||||
|
// do stuff
|
||||||
|
}
|
||||||
|
|
||||||
|
$client->logout();
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Config
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# pwsclient config
|
||||||
|
|
||||||
|
# 0 : disable, 1 : verboose, 2 : most verboose
|
||||||
|
debug : 1
|
||||||
|
protocol : https://
|
||||||
|
hostname : pwsserver.docker
|
||||||
|
# file storing the server 's session id - must be out of DocumentRoot and read/writable by server
|
||||||
|
store : wsess
|
||||||
|
login : test
|
||||||
|
password : test
|
||||||
|
key : test
|
||||||
|
# 0 : display cli, 1 : display html
|
||||||
|
html_output : 0
|
||||||
|
# http authentication
|
||||||
|
http :
|
||||||
|
user :
|
||||||
|
password :
|
||||||
|
# server uris for authentication
|
||||||
|
uri :
|
||||||
|
auth : /ws/auth
|
||||||
|
logout : /ws/logout
|
||||||
|
check : /ws/isauth
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Server Response
|
||||||
|
|
||||||
|
PwsClient intend to receiv any JsonResponse, the structure of the response is free.
|
||||||
|
However, meta-tech always return this simple Json Structure :
|
||||||
|
{ done : boolean, msg : 'string contextual msg', data : whatever }
|
||||||
|
|
||||||
|
|
||||||
|
### License
|
||||||
|
|
||||||
|
The project is released under the MIT license, see the LICENSE file.
|
29
composer.json
Normal file
29
composer.json
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
{
|
||||||
|
"name" : "meta-tech/pws-client",
|
||||||
|
"type" : "library",
|
||||||
|
"homepage" : "https://github.com/meta-tech/pws-client",
|
||||||
|
"description" : "PwsClient is a php web service client managing PwsAuth protocol",
|
||||||
|
"license" : "MIT",
|
||||||
|
"authors" : [
|
||||||
|
{
|
||||||
|
"name" : "a-Sansara",
|
||||||
|
"homepage" : "https://github.com/a-sansara/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"keywords" : ["Client", "PwsAuth", "WebService", "Http"],
|
||||||
|
"autoload" : {
|
||||||
|
"psr-4" : {
|
||||||
|
"" : "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"require" : {
|
||||||
|
"symfony/yaml" : "^3.2",
|
||||||
|
"meta-tech/pws-auth": "@dev"
|
||||||
|
},
|
||||||
|
"repositories" : [
|
||||||
|
{
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/meta-tech/pws-auth.git"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
24
config/pwsauth.yml.dist
Normal file
24
config/pwsauth.yml.dist
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# pwsauth config
|
||||||
|
|
||||||
|
type : PwsAuth2
|
||||||
|
|
||||||
|
header :
|
||||||
|
auth : Pws-Authorization
|
||||||
|
ident : Pws-Ident
|
||||||
|
|
||||||
|
salt :
|
||||||
|
common : jK5#p9Mh5.Zv}
|
||||||
|
# used for generating user specific salt
|
||||||
|
user.index : 10
|
||||||
|
user.length : 12
|
||||||
|
|
||||||
|
hash :
|
||||||
|
sep : /
|
||||||
|
algo : sha256
|
||||||
|
# effective token length size. out of bound data is simply noise
|
||||||
|
length : 52
|
||||||
|
# session index (or obfuscate length)
|
||||||
|
session.index : 58
|
||||||
|
# ending noise data length)
|
||||||
|
noise.length : 12
|
||||||
|
|
22
config/pwsclient.yml.dist
Normal file
22
config/pwsclient.yml.dist
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# pwsclient config
|
||||||
|
|
||||||
|
# 0 : disable, 1 : verboose, 2 : most verboose
|
||||||
|
debug : 1
|
||||||
|
protocol : https://
|
||||||
|
hostname : pwsserver.docker
|
||||||
|
# file storing the server 's session id - must be out of DocumentRoot and read/writable by server
|
||||||
|
store : wsess
|
||||||
|
login : test
|
||||||
|
password : test
|
||||||
|
key : test
|
||||||
|
# 0 : display cli, 1 : display html
|
||||||
|
html_output : 0
|
||||||
|
# http authentication
|
||||||
|
http :
|
||||||
|
user :
|
||||||
|
password :
|
||||||
|
# server uris for authentication
|
||||||
|
uri :
|
||||||
|
auth : /ws/auth
|
||||||
|
logout : /ws/logout
|
||||||
|
check : /ws/isauth
|
136
src/MetaTech/Output/Formatter.php
Normal file
136
src/MetaTech/Output/Formatter.php
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of the pws-client package.
|
||||||
|
*
|
||||||
|
* (c) meta-tech.academy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
namespace MetaTech\Output;
|
||||||
|
|
||||||
|
use MetaTech\Util\Tool;
|
||||||
|
use MetaTech\PwsAuth\Authenticator;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* PwsAuth token
|
||||||
|
*
|
||||||
|
* @package MetaTech\Output
|
||||||
|
* @class class Formatter
|
||||||
|
* @author a-Sansara
|
||||||
|
* @date 2017-03-11 20:09:24 CET
|
||||||
|
*/
|
||||||
|
class Formatter
|
||||||
|
{
|
||||||
|
/*! @constant TYPE_CLI */
|
||||||
|
const TYPE_CLI = 0;
|
||||||
|
/*! @constant TYPE_HTML */
|
||||||
|
const TYPE_HTML = 1;
|
||||||
|
/*! @constant TYPE_JSON */
|
||||||
|
const TYPE_JSON = 2;
|
||||||
|
/*! @constant CSS_PREFIX_TAG */
|
||||||
|
const CSS_PREFIX_TAG = 'meta-tech-of-tag';
|
||||||
|
/*! @constant CSS_NAME_LINE */
|
||||||
|
const CSS_NAME_LINE = 'meta-tech-of-line';
|
||||||
|
/*! @constant LF */
|
||||||
|
const LF = "
|
||||||
|
";
|
||||||
|
/*! @protected @var int $type */
|
||||||
|
protected $type;
|
||||||
|
/*! @protected @var int $type */
|
||||||
|
protected $embedStyle;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* desc
|
||||||
|
*
|
||||||
|
* @constructor
|
||||||
|
* @public
|
||||||
|
* @param int $type
|
||||||
|
*/
|
||||||
|
public function __construct($type = Formatter::TYPE_CLI)
|
||||||
|
{
|
||||||
|
$this->type = $type;
|
||||||
|
$this->embedStyle = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* desc
|
||||||
|
*
|
||||||
|
* @method _initCall
|
||||||
|
* @private
|
||||||
|
* @param str $uri the web service uri
|
||||||
|
* @return $curl instance
|
||||||
|
*/
|
||||||
|
private function format($value, $color=null)
|
||||||
|
{
|
||||||
|
if ($this->type == self::TYPE_HTML) {
|
||||||
|
$tag = $color === false ? '' : (is_null($color) ? '</span>' : '<span class="' . self::CSS_PREFIX_TAG . $color . '">');
|
||||||
|
$content = $tag . $value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$tag = $color === false ? '' : (is_null($color) ? "\033[0m" : "\033[1;3".$color."m" );
|
||||||
|
$content = $tag . $value;
|
||||||
|
}
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @method getHtmlClass
|
||||||
|
* @private
|
||||||
|
* @param int $idColor
|
||||||
|
* @return str
|
||||||
|
*/
|
||||||
|
public function embedStyleIfNeeded()
|
||||||
|
{
|
||||||
|
$style = '';
|
||||||
|
if (!$this->embedStyle) {
|
||||||
|
$this->embedStyle = true;
|
||||||
|
$style .= '<style type="text/css">'
|
||||||
|
. '.meta-tech-of-line { white-space:pre !important; background-color:black !important; color:white !important; font-weight:bold !important; font-family:monospace !important; padding:10px !important; margin-top:0 !important }'
|
||||||
|
. '.meta-tech-of-tag1 { color:#FB4E4E !important; }'
|
||||||
|
. '.meta-tech-of-tag2 { color:#20FF93 !important; }'
|
||||||
|
. '.meta-tech-of-tag3 { color:#FFDC58 !important; }'
|
||||||
|
. '.meta-tech-of-tag4 { color:#44A2D6 !important; }'
|
||||||
|
. '</style>';
|
||||||
|
}
|
||||||
|
return $style;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @method writeTags
|
||||||
|
* @private
|
||||||
|
* @param [] $tags
|
||||||
|
*/
|
||||||
|
public function writeTags($tags)
|
||||||
|
{
|
||||||
|
$content = '';
|
||||||
|
foreach($tags as $tag) {
|
||||||
|
if (!empty($tag)) {
|
||||||
|
if (is_array($tag)) {
|
||||||
|
$content .= $this->format($tag[0], count($tag) > 1 ? $tag[1] : null);
|
||||||
|
}
|
||||||
|
elseif (is_string($tag)) {
|
||||||
|
$content .= $this->format($tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->write($content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @method write
|
||||||
|
* @private
|
||||||
|
* @param str $value
|
||||||
|
* @param bool $newline
|
||||||
|
* @return str
|
||||||
|
*/
|
||||||
|
private function write($value, $newline=true)
|
||||||
|
{
|
||||||
|
$content = $value . ($newline ? self::LF : '');
|
||||||
|
if ($this->type == self::TYPE_HTML) {
|
||||||
|
$content = $this->embedStyleIfNeeded() . '<div class"meta-tech-ofline">'.$content.'</div>';
|
||||||
|
}
|
||||||
|
echo $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
335
src/MetaTech/Ws/Client.php
Normal file
335
src/MetaTech/Ws/Client.php
Normal file
|
@ -0,0 +1,335 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of the pws-client package.
|
||||||
|
*
|
||||||
|
* (c) meta-tech.academy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
namespace MetaTech\Ws;
|
||||||
|
|
||||||
|
use MetaTech\Util\Tool;
|
||||||
|
use MetaTech\PwsAuth\Authenticator;
|
||||||
|
use MetaTech\Output\Formatter;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* PwsAuth token
|
||||||
|
*
|
||||||
|
* @package MetaTech\Ws
|
||||||
|
* @class PwsClient
|
||||||
|
* @author a-Sansara
|
||||||
|
* @date 2016-05-02 13:19:01 CET
|
||||||
|
*/
|
||||||
|
class Client
|
||||||
|
{
|
||||||
|
/*! @constant QUIET */
|
||||||
|
const QUIET = 0;
|
||||||
|
/*! @constant VERBOOSE */
|
||||||
|
const VERBOOSE = 1;
|
||||||
|
/*! @constant MOST_VERBOOSE */
|
||||||
|
const MOST_VERBOOSE = 2;
|
||||||
|
/*! @constant LF */
|
||||||
|
const LF = "
|
||||||
|
";
|
||||||
|
|
||||||
|
/*! @protected @var [assoc] $config */
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/*! @protected @var MetaTech\Output\Formatter $formatter */
|
||||||
|
protected $formatter;
|
||||||
|
|
||||||
|
/*! @protected @var Mtc\Core\Auth\Authenticator $authenticator */
|
||||||
|
protected $authenticator;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* desc
|
||||||
|
*
|
||||||
|
* @constructor
|
||||||
|
* @param [assoc] $config
|
||||||
|
* @param MetaTech\PwsAuth\Authenticator $authenticator
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
public function __construct($config, Authenticator $authenticator)
|
||||||
|
{
|
||||||
|
if (!is_array($config)) {
|
||||||
|
throw new \Exception('bad rest config');
|
||||||
|
}
|
||||||
|
$typeFormatter = $this->config['html_output'] ? Formatter::TYPE_HTML : Formatter::TYPE_CLI;
|
||||||
|
$this->formatter = new Formatter($typeFormatter);
|
||||||
|
$this->config = $config;
|
||||||
|
$this->authenticator = $authenticator;
|
||||||
|
|
||||||
|
if ($this->config['debug'] && $this->config['debug'] == self::MOST_VERBOOSE) {
|
||||||
|
$config['password'] = '--hidden--';
|
||||||
|
$config['key'] = substr($config['key'], 0, 3) . '...--hidden--';
|
||||||
|
if (!empty($config['http']['password'])) {
|
||||||
|
$config['http']['password'] = '--hidden--';
|
||||||
|
}
|
||||||
|
$this->formatter->writeTags([
|
||||||
|
[date('H:i:s') , '3'],
|
||||||
|
[' [' . __class__ . ']' , '1'],
|
||||||
|
[' debug mode verboose, view config :', '2'],
|
||||||
|
Formatter::LF,
|
||||||
|
[var_export($config, true), null]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
if (!file_exists($config['store'])) {
|
||||||
|
$this->_persist('#');
|
||||||
|
}
|
||||||
|
$resp = $this->check();
|
||||||
|
if (is_null($resp) || !$resp->done) {
|
||||||
|
$this->auth();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* call the authenticate web service
|
||||||
|
*
|
||||||
|
* @method auth
|
||||||
|
* @public
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function auth()
|
||||||
|
{
|
||||||
|
$header = $this->_buildHeader();
|
||||||
|
$data = Tool::compact($this->config, ['login', 'password']);
|
||||||
|
$data = $this->_send($this->config['uri']['auth'], $data, "POST", $header);
|
||||||
|
$resp = $data['response'];
|
||||||
|
if ($resp!=null && $resp->done) {
|
||||||
|
$this->_persist($resp->data->sid);
|
||||||
|
}
|
||||||
|
unset($resp->data->sid);
|
||||||
|
return $resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* call the logout web service
|
||||||
|
*
|
||||||
|
* @method logout
|
||||||
|
* @public
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public function logout()
|
||||||
|
{
|
||||||
|
return $this->get($this->config['uri']['logout']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* call the check auth web service
|
||||||
|
*
|
||||||
|
* @method check
|
||||||
|
* @public
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public function check()
|
||||||
|
{
|
||||||
|
return $this->get($this->config['uri']['check']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* call a web service denotes by $uri
|
||||||
|
*
|
||||||
|
* @method get
|
||||||
|
* @public
|
||||||
|
* @param str $uri the web service uri
|
||||||
|
* @param [] $data the post data
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function get($uri)
|
||||||
|
{
|
||||||
|
$header = $this->_buildHeader($this->_getToken());
|
||||||
|
$resp = $this->_send($uri, null, 'GET', $header);
|
||||||
|
return $resp['response'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* call a web service denotes by $uri with $data post
|
||||||
|
*
|
||||||
|
* @method post
|
||||||
|
* @public
|
||||||
|
* @param str $uri the web service uri
|
||||||
|
* @param [] $data the post data
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function post($uri, $data=array(), $trace=false)
|
||||||
|
{
|
||||||
|
$header = $this->_buildHeader($this->_getToken());
|
||||||
|
$resp = $this->_send($uri, $data, 'POST', $header);
|
||||||
|
return $resp['response'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* build header for authentication. if token is missing or null, the header is
|
||||||
|
* specific to a login action, overwise the header is specific to a loading session
|
||||||
|
*
|
||||||
|
* @method _buildHeader
|
||||||
|
* @private
|
||||||
|
* @param str $login
|
||||||
|
* @param str $key
|
||||||
|
* @param str $sessid
|
||||||
|
* @retval [str]
|
||||||
|
*/
|
||||||
|
private function _buildHeader($sessid=null)
|
||||||
|
{
|
||||||
|
$header = $this->authenticator->generateHeader($this->config['login'], $this->config['key'], $sessid);
|
||||||
|
return $header;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* desc
|
||||||
|
*
|
||||||
|
* @method _persist
|
||||||
|
* @private
|
||||||
|
* @param str $token the session token
|
||||||
|
*/
|
||||||
|
private function _persist($token)
|
||||||
|
{
|
||||||
|
file_put_contents($this->config['store'], $token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* desc
|
||||||
|
*
|
||||||
|
* @method _getToken
|
||||||
|
* @private
|
||||||
|
* @return str
|
||||||
|
*/
|
||||||
|
private function _getToken()
|
||||||
|
{
|
||||||
|
return file_get_contents($this->config['store']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* desc
|
||||||
|
*
|
||||||
|
* @method _initCall
|
||||||
|
* @private
|
||||||
|
* @param str $uri the web service uri
|
||||||
|
* @return $curl instance
|
||||||
|
*/
|
||||||
|
private function _initCall($uri, $method)
|
||||||
|
{
|
||||||
|
$curl = curl_init();
|
||||||
|
$url = $this->config['protocol'] . $this->config['hostname'] . $uri;
|
||||||
|
if ($this->config['debug'] == self::MOST_VERBOOSE) {
|
||||||
|
$this->formatter->writeTags([
|
||||||
|
[date('H:i:s') , '3'],
|
||||||
|
[' [' . __class__ . ']', '1'],
|
||||||
|
[" => $method $url" , '2'],
|
||||||
|
''
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
curl_setopt($curl, CURLOPT_URL , $url);
|
||||||
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
||||||
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($curl, CURLOPT_HEADER , true);
|
||||||
|
curl_setopt($curl, CURLOPT_COOKIESESSION , false);
|
||||||
|
curl_setopt($curl, CURLOPT_USERAGENT , $this->config['key']);
|
||||||
|
if (preg_match('/^https:/i', $this->config['protocol'])) {
|
||||||
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
|
||||||
|
}
|
||||||
|
if (isset($this->config['http']) && isset($this->config['http']['user']) && isset($this->config['http']['password'])) {
|
||||||
|
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
|
||||||
|
curl_setopt($curl, CURLOPT_USERPWD , $this->config['http']['user'] . ':'. $this->config['http']['password']);
|
||||||
|
}
|
||||||
|
return $curl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @method _send
|
||||||
|
* @private
|
||||||
|
* @param str $uri the web service uri to call
|
||||||
|
* @param []|null $data the post params
|
||||||
|
* @param str $method the webservice method
|
||||||
|
* @param bool $header the header to send
|
||||||
|
* @retval [assoc]
|
||||||
|
*/
|
||||||
|
private function _send($uri, $data=null, $method="GET", $header=array())
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$stime = microtime(true);
|
||||||
|
$date = Tool::dateFromTime($stime);
|
||||||
|
$curl = $this->_initCall($uri, $method);
|
||||||
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST , $method);
|
||||||
|
if ($method == "POST") {
|
||||||
|
curl_setopt($curl, CURLOPT_POST, true);
|
||||||
|
if ($data!=null && !empty($data)) {
|
||||||
|
$fields = http_build_query($data);
|
||||||
|
$header[] = 'Content-Length: ' . strlen($fields);
|
||||||
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count($header) > 0) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
||||||
|
// curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($this, "HandleHeaderLine"));
|
||||||
|
$rs = curl_exec($curl);
|
||||||
|
$exectime = number_format(((microtime(true)-$stime)),5);
|
||||||
|
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||||
|
$size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
|
||||||
|
$respheader = substr($rs, 0, $size);
|
||||||
|
$body = substr($rs, $size);
|
||||||
|
$response = json_decode($body);
|
||||||
|
$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
|
||||||
|
if ($status == 0) {
|
||||||
|
throw new \Exception(curl_error($curl));
|
||||||
|
}
|
||||||
|
curl_close($curl);
|
||||||
|
}
|
||||||
|
catch(\Exception $e) {
|
||||||
|
if (isset($curl)) {
|
||||||
|
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||||
|
$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
|
||||||
|
$error = curl_error($curl);
|
||||||
|
$response = [ 'done' => false, 'msg' => ' CURL ERROR : '.$error.' - status : '.$status.' - url : '.$url ];
|
||||||
|
curl_close($curl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($this->config['debug']) {
|
||||||
|
$respcontent = null;
|
||||||
|
if (is_object($response)) {
|
||||||
|
$respcontent = clone $response;
|
||||||
|
if (isset($respcontent->data) && $this->config['html_output']) {
|
||||||
|
if (is_string($respcontent->data)) {
|
||||||
|
$respcontent->data = htmlentities($respcontent->data);
|
||||||
|
}
|
||||||
|
elseif (isset($respcontent->data->html) && is_string($respcontent->data->html)) {
|
||||||
|
$respcontent->data = clone $response->data;
|
||||||
|
$respcontent->data->html = htmlentities($respcontent->data->html);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$tags = [
|
||||||
|
[date('H:i:s') , 3],
|
||||||
|
[' ['. __class__ .']' , 1],
|
||||||
|
[($this->config['debug'] == self::MOST_VERBOOSE ? " <=" : "")." $method $url" , 2],
|
||||||
|
[" $exectime s " , 4],
|
||||||
|
['', null],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
switch ($this->config['debug']) {
|
||||||
|
|
||||||
|
case self::MOST_VERBOOSE :
|
||||||
|
if (isset($data['password'])) {
|
||||||
|
$data['password'] = '--hidden--';
|
||||||
|
}
|
||||||
|
$traces = var_export([
|
||||||
|
'HEADER' => $this->authenticator->readHeader($header),
|
||||||
|
'PARAMS' => $data,
|
||||||
|
'METHOD' => $method,
|
||||||
|
'RESPONSE' => compact('date', 'uri', 'status') + ['curl' => $rs, 'response' => $respcontent]
|
||||||
|
], true) . Formatter::LF;
|
||||||
|
array_unshift($tags, $traces);
|
||||||
|
$this->formatter->writeTags($tags);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case self::VERBOOSE :
|
||||||
|
array_unshift($tags, Formatter::LF);
|
||||||
|
$tags[] = var_export(compact('status')+['response' => $respcontent], true);
|
||||||
|
$this->formatter->writeTags($tags);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return compact('date', 'uri', 'response', 'status', 'exectime');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user