JBZoo\Utils\Url::addArg PHP Method

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
    public static function addArg(array $newParams, $uri = null)
    {
        $uri = is_null($uri) ? Vars::get($_SERVER['REQUEST_URI'], '') : $uri;
        // Parse the URI into it's components
        $puri = parse_url($uri);
        if (Arr::key('query', $puri)) {
            parse_str($puri['query'], $queryParams);
            $queryParams = array_merge($queryParams, $newParams);
        } elseif (Arr::key('path', $puri) && strstr($puri['path'], '=') !== false) {
            $puri['query'] = $puri['path'];
            unset($puri['path']);
            parse_str($puri['query'], $queryParams);
            $queryParams = array_merge($queryParams, $newParams);
        } else {
            $queryParams = $newParams;
        }
        // Strip out any query params that are set to false.
        // Properly handle valueless parameters.
        foreach ($queryParams as $param => $value) {
            if ($value === false) {
                unset($queryParams[$param]);
            } elseif ($value === null) {
                $queryParams[$param] = '';
            }
        }
        // Re-construct the query string
        $puri['query'] = self::build($queryParams);
        // Strip = from valueless parameters.
        $puri['query'] = preg_replace('/=(?=&|$)/', '', $puri['query']);
        // Re-construct the entire URL
        $nuri = self::buildAll($puri);
        // Make the URI consistent with our input
        foreach (array('/', '?') as $char) {
            if ($nuri[0] === $char && strstr($uri, $char) === false) {
                $nuri = substr($nuri, 1);
            }
        }
        return rtrim($nuri, '?');
    }

Usage Example

コード例 #1
0
ファイル: Guzzle5.php プロジェクト: jbzoo/http-client
 /**
  * @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;
 }
All Usage Examples Of JBZoo\Utils\Url::addArg