I have a scraping object basically. I want to be able to add POST variables to it like
$obj->addvar('Name', 'Value');
What I have now is this:
function addvar($var, $val) {
$postvars[] = Array($var=>$val);
}
function initiate() {
$this->q = $postvars;
}
if(!empty($this->post)) {
$this->params = http_build_query($q);
}
I haven’t tested because it’s too incomplete, But would my addvar() function work? How on earth do I append a key+value to the array so http_build_query would accept it?
IE (this is what I want):
$obj->addvar('username', 'abc');
$obj->addvar('password', 'foobar');
$obj->send(); //..
You have several issues in your code:
addvarmethod, you are not accessing any instance variables. You are assigning the alues to a local variable.initiatemethod cannot access the variable$postvar.ifclause you are accessing a local variable$qinstead of the instance variable$this->q.http_build_querybut is has to be a “normal” array.You are mixing up a lot!
A more complete example of your class would be helpful, but I think it should look more like this:
Example:
In general, if you already have the query that was built by
html_build_queryyou can just append to that string: