Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何动态添加到http_构建_查询?_Php_Http_Arrays - Fatal编程技术网

PHP:如何动态添加到http_构建_查询?

PHP:如何动态添加到http_构建_查询?,php,http,arrays,Php,Http,Arrays,我基本上有一个刮东西。我希望能够像这样添加POST变量 $obj->addvar('Name', 'Value'); 我现在得到的是: function addvar($var, $val) { $postvars[] = Array($var=>$val); } function initiate() { $this->q = $postvars; } if(!empty($this->post)) { $this->

我基本上有一个刮东西。我希望能够像这样添加POST变量

$obj->addvar('Name', 'Value');
我现在得到的是:

  function addvar($var, $val) {
    $postvars[] = Array($var=>$val); 
  }
  function initiate() {
    $this->q = $postvars;
  }
  if(!empty($this->post)) {
    $this->params = http_build_query($q);
  }
我没有测试,因为它太不完整了,但是我的addvar()函数可以工作吗?我究竟如何将key+值附加到数组中,以便http\u build\u查询接受它

IE(这就是我想要的):

你可以做:

$postvars[$var] = $val;
显然,您需要确保在数组中包含所有值后调用
http\u build\u query()


另外,
$postvars
看起来像一个局部变量,因此它只在该方法中可见(并且在每次调用时都会重置)。最好使其成为类的一个成员。

此处的addvars代码有一些问题($this->params似乎不在函数initiate()的范围内),否则它应该可以正常工作

class test{
  var $postvars;
  function addvar($var, $val) {
    $this->postvars[] = Array($var=>$val); 
  }
  function initiate() {
    $this->q = $this->postvars;
    return http_build_query($this->q);
  }
}

    $obj = new test();
    $test->addvars('username', 'abc');

    $qry = $test->initiate();

这应该行得通。但未经测试。

您的代码中有几个问题:

  • addvar
    方法中,您没有访问任何实例变量。您正在为局部变量赋值
  • 您的
    initiate
    方法无法访问变量
    $postvar
  • if
    子句中,您正在访问局部变量
    $q
    ,而不是实例变量
    $this->q
  • 您希望将一个数组传递给
    http\u build\u query
    ,但它必须是一个“普通”数组
你把事情搞混了

一个更完整的类示例可能会有所帮助,但我认为它应该更像这样:

class QueryBuilder {
    private $params = array();

    public function addParameter($key, $value) {
        $this->params[$key] = $value;
    }

    public function send() {
        $query = http_build_query($this->params);
        // whatever else has to be done to send.
        // for the sake of this example, it just returns the query string:
        return $query;
    }
}
例如:

$obj = new QueryBuilder();
$obj->addParameter('username', 'abc');
$obj->addParameter('password', 'foobar');
echo $obj->send(); // echos 'username=abc&password=foobar'

通常,如果您已经拥有由
html\u build\u query
生成的查询,您可以将其附加到该字符串:

$query = http_build_query(array('foo' => 'bar', 'faa' => 'baz'));
$query .= '&key=value';
echo $query; // echos 'foo=bar&faa=baz&key=value'

这为我节省了很多,我在PHP方面不是很差,但是类,从来没有使用过它们。但现在在这种情况下似乎很神奇,伙计,我把事情搞混了。它工作得非常好,谢谢。@oni-kun:如果您是OOP新手,请阅读介绍:它会给您一些见解;)
$query = http_build_query(array('foo' => 'bar', 'faa' => 'baz'));
$query .= '&key=value';
echo $query; // echos 'foo=bar&faa=baz&key=value'