addArg() public static method

Add or remove query arguments to the URL.
public static addArg ( array $newParams, mixed $uri = null ) : string
$newParams array Either newkey or an associative array
$uri mixed URI or URL to append the queru/queries to.
return string
Beispiel #1
0
 /**
  * @inheritdoc
  */
 public function multiRequest(array $urls)
 {
     $client = new Client();
     $requests = array();
     foreach ($urls as $urlName => $urlData) {
         if (is_string($urlData)) {
             $urlData = array($urlData, array());
         }
         $urlOptions = new Options($urlData[1]);
         $method = $urlOptions->get('method', 'GET', 'up');
         $args = $urlOptions->get('args');
         $url = 'GET' === $method ? Url::addArg((array) $args, $urlData[0]) : $urlData[0];
         $requests[$urlName] = $client->createRequest($method, $url, $this->_getClientOptions($urlOptions, $method, $args));
     }
     $httpResults = Pool::batch($client, $requests);
     /** @var string $resName */
     /** @var Response $httpResult */
     $result = array();
     $index = 0;
     $keys = array_keys($urls);
     foreach ($keys as $resName) {
         $httpResult = $httpResults->offsetGet($index++);
         $result[$resName] = array($httpResult->getStatusCode(), $httpResult->getHeaders(), $httpResult->getBody()->getContents());
     }
     return $result;
 }
Beispiel #2
0
 /**
  * @inheritdoc
  */
 public function multiRequest(array $urls)
 {
     $requests = array();
     foreach ($urls as $urlName => $urlData) {
         if (is_string($urlData)) {
             $urlData = array($urlData, array());
         }
         $urlOptions = new Options($urlData[1]);
         $method = $urlOptions->get('method', 'GET', 'up');
         $args = $urlOptions->get('args');
         $url = 'GET' === $method ? Url::addArg((array) $args, $urlData[0]) : $urlData[0];
         $args = 'GET' !== $method ? $args : array();
         $requests[$urlName] = array('url' => $url, 'headers' => $urlOptions->getHeaders(), 'data' => $args, 'type' => $method, 'options' => $this->_getClientOptions($urlOptions));
     }
     $httpResults = \Requests::request_multiple($requests);
     /** @var string $resName */
     /** @var \Requests_Response $httpResult */
     $result = array();
     foreach ($httpResults as $resName => $httpResult) {
         $result[$resName] = array($httpResult->status_code, $httpResult->headers->getAll(), $httpResult->body);
     }
     return $result;
 }
Beispiel #3
0
 /**
  * @param string            $url
  * @param string|array|null $args
  * @param string            $method
  * @param array             $options
  * @return Response
  * @throws Exception
  */
 public function request($url, $args = null, $method = Options::DEFAULT_METHOD, array $options = array())
 {
     $method = Filter::up($method);
     $url = 'GET' === $method ? Url::addArg((array) $args, $url) : $url;
     $options = new Options(array_merge($this->_options->getArrayCopy(), $options));
     $client = $this->_getClient($options);
     $response = new Response();
     try {
         list($code, $headers, $body) = $client->request($url, $args, $method, $options);
         $response->setCode($code);
         $response->setHeaders($headers);
         $response->setBody($body);
     } catch (\Exception $e) {
         if ($options->isExceptions()) {
             throw new Exception($e->getMessage(), $e->getCode(), $e);
         } else {
             $response->setCode($e->getCode());
             $response->setHeaders(array());
             $response->setBody($e->getMessage());
         }
     }
     return $response;
 }
Beispiel #4
0
 /**
  * @inheritdoc
  */
 public function multiRequest(array $urls)
 {
     $client = new Client();
     $promises = array();
     foreach ($urls as $urlName => $urlData) {
         if (is_string($urlData)) {
             $urlData = array($urlData, array());
         }
         $urlOptions = new Options($urlData[1]);
         $method = $urlOptions->get('method', 'GET', 'up');
         $args = $urlOptions->get('args');
         $url = 'GET' === $method ? Url::addArg((array) $args, $urlData[0]) : $urlData[0];
         $promises[$urlName] = $client->requestAsync($method, $url, $this->_getClientOptions($urlOptions, $method, $args));
     }
     $httpResults = Promise\unwrap($promises);
     /** @var string $resName */
     /** @var Response $httpResult */
     $result = array();
     foreach ($httpResults as $resName => $httpResult) {
         $result[$resName] = array($httpResult->getStatusCode(), $httpResult->getHeaders(), $httpResult->getBody()->getContents());
     }
     return $result;
 }
Beispiel #5
0
 public function testAddArg()
 {
     // Regular tests
     is('user=5', Url::addArg(array('user' => 5), ''));
     is('/app/admin/users?user=5', Url::addArg(array('user' => 5), '/app/admin/users'));
     is('/app/admin/users?action=edit&user=5', Url::addArg(array('user' => 5), '/app/admin/users?action=edit'));
     is('/app/admin/users?action=edit&tab=personal&user=5', Url::addArg(array('user' => 5), '/app/admin/users?action=edit&tab=personal'));
     // Ensure strips false.
     is('/index.php', Url::addArg(array('debug' => false), '/index.php'));
     // With valueless parameters.
     is('/index.php?debug', Url::addArg(array('debug' => null), '/index.php'));
     is('/index.php?debug#hash', Url::addArg(array('debug' => null), '/index.php#hash'));
     // With a URL fragment
     is('/app/admin/users?user=5#test', Url::addArg(array('user' => 5), '/app/admin/users#test'));
     // Full URL
     is('http://example.com/?a=b', Url::addArg(array('a' => 'b'), 'http://example.com'));
     // Only the query string
     is('?a=b&c=d', Url::addArg(array('c' => 'd'), '?a=b'));
     is('a=b&c=d', Url::addArg(array('c' => 'd'), 'a=b'));
     // Url encoding test
     is('/app/admin/users?param=containsa%26sym', Url::addArg(array('param' => 'containsa&sym'), '/app/admin/users'));
     // If not provided, grab the URI from the server.
     $_SERVER['REQUEST_URI'] = '/app/admin/users';
     is('/app/admin/users?user=6', Url::addArg(array('user' => 6)));
     is('/app/admin/users?user=7', Url::addArg(array('user' => 7)));
 }
Beispiel #6
0
 public function testRedirect()
 {
     $url = Url::addArg(array('url' => 'http://example.com'), 'http://httpbin.org/redirect-to');
     $result = $this->_getClient()->request($url);
     isSame(200, $result->code);
     isContain('text/html', $result->find('headers.content-type'));
     isContain('Example', $result->body);
 }
Beispiel #7
0
 /**
  * {@inheritdoc}
  */
 public function request($url, $args = array(), array $options = array())
 {
     $result = null;
     // Merge with default
     $options = array_merge($this->_defaultOptions, (array) $options);
     $options = new Data($options);
     // Prepare options for request
     $args = is_string($args) ? $args : (array) $args;
     $timeout = (int) $options->get('timeout');
     $isDebug = (int) $options->get('debug');
     $sslVerify = (int) $options->get('ssl_verify');
     $headers = (array) $options->get('headers');
     $userAgent = trim($options->get('user_agent'));
     $resultType = Str::clean($options->get('response'), true);
     // Prepare options for cache
     $isCache = (int) $options->get('cache');
     $cacheTTL = (int) $options->get('cache_ttl');
     $cacheId = array('url' => $url, 'data' => $args, 'options' => $options->getArrayCopy());
     try {
         // Check cache
         if ($isCache && ($result = $this->_cms['cache']->get($cacheId, self::CACHE_GROUP))) {
             return $result;
         }
         $method = $this->_getMethod($options->get('method'));
         // Add args to url for GET methods
         if (self::METHOD_GET === $method) {
             $url = Url::addArg($args, $url);
             $args = array();
         }
         // Request via CMS API
         $apiResp = $this->_request($url, $args, new Data(array('timeout' => $timeout, 'headers' => $headers, 'method' => $method, 'debug' => $isDebug, 'user_agent' => $userAgent, 'ssl_verify' => $sslVerify)));
     } catch (\Exception $e) {
         $apiResp = new Data(array('body' => 'CrossCMS Error: ' . $e->getMessage() . PHP_EOL . PHP_EOL . $e->getTraceAsString(), 'headers' => array(), 'code' => 0));
     }
     // Prepare response format
     $response = $this->_compactResponse($apiResp);
     $result = $this->_getResultByType($response, $resultType);
     // Store to cache
     if ($isCache && null !== $result) {
         $this->_cms['cache']->set($cacheId, $result, self::CACHE_GROUP, true, $cacheTTL);
     }
     return $result;
 }