2023-03-11 12:04:29 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Zend Framework (http://framework.zend.com/)
|
|
|
|
*
|
|
|
|
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
2023-04-01 09:03:34 +03:00
|
|
|
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
2023-03-11 12:04:29 +03:00
|
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Zend\Http;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Http static client
|
|
|
|
*/
|
|
|
|
class ClientStatic
|
|
|
|
{
|
2023-04-01 09:03:34 +03:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
2023-03-11 12:04:29 +03:00
|
|
|
protected static $client;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the static HTTP client
|
|
|
|
*
|
2023-04-01 09:03:34 +03:00
|
|
|
* @param array|Traversable $options
|
2023-03-11 12:04:29 +03:00
|
|
|
* @return Client
|
|
|
|
*/
|
2023-04-01 09:03:34 +03:00
|
|
|
protected static function getStaticClient($options = null)
|
2023-03-11 12:04:29 +03:00
|
|
|
{
|
2023-04-01 09:03:34 +03:00
|
|
|
if (!isset(static::$client) || $options !== null) {
|
|
|
|
static::$client = new Client(null, $options);
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
return static::$client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP GET METHOD (static)
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param array $query
|
|
|
|
* @param array $headers
|
|
|
|
* @param mixed $body
|
2023-04-01 09:03:34 +03:00
|
|
|
* @param array|Traversable $clientOptions
|
2023-03-11 12:04:29 +03:00
|
|
|
* @return Response|bool
|
|
|
|
*/
|
2023-04-01 09:03:34 +03:00
|
|
|
public static function get($url, $query = array(), $headers = array(), $body = null, $clientOptions = null)
|
2023-03-11 12:04:29 +03:00
|
|
|
{
|
|
|
|
if (empty($url)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$request= new Request();
|
|
|
|
$request->setUri($url);
|
|
|
|
$request->setMethod(Request::METHOD_GET);
|
|
|
|
|
|
|
|
if (!empty($query) && is_array($query)) {
|
|
|
|
$request->getQuery()->fromArray($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($headers) && is_array($headers)) {
|
|
|
|
$request->getHeaders()->addHeaders($headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($body)) {
|
|
|
|
$request->setContent($body);
|
|
|
|
}
|
|
|
|
|
2023-04-01 09:03:34 +03:00
|
|
|
return static::getStaticClient($clientOptions)->send($request);
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP POST METHOD (static)
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param array $params
|
|
|
|
* @param array $headers
|
|
|
|
* @param mixed $body
|
2023-04-01 09:03:34 +03:00
|
|
|
* @param array|Traversable $clientOptions
|
2023-03-11 12:04:29 +03:00
|
|
|
* @throws Exception\InvalidArgumentException
|
|
|
|
* @return Response|bool
|
|
|
|
*/
|
2023-04-01 09:03:34 +03:00
|
|
|
public static function post($url, $params, $headers = array(), $body = null, $clientOptions = null)
|
2023-03-11 12:04:29 +03:00
|
|
|
{
|
|
|
|
if (empty($url)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$request= new Request();
|
|
|
|
$request->setUri($url);
|
|
|
|
$request->setMethod(Request::METHOD_POST);
|
|
|
|
|
|
|
|
if (!empty($params) && is_array($params)) {
|
|
|
|
$request->getPost()->fromArray($params);
|
|
|
|
} else {
|
|
|
|
throw new Exception\InvalidArgumentException('The array of post parameters is empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($headers['Content-Type'])) {
|
2023-04-01 09:03:34 +03:00
|
|
|
$headers['Content-Type'] = Client::ENC_URLENCODED;
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($headers) && is_array($headers)) {
|
|
|
|
$request->getHeaders()->addHeaders($headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($body)) {
|
|
|
|
$request->setContent($body);
|
|
|
|
}
|
|
|
|
|
2023-04-01 09:03:34 +03:00
|
|
|
return static::getStaticClient($clientOptions)->send($request);
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
}
|