简体   繁体   中英

create function that support GET,POST and Command line arguments

I have a following class file. When valid_orginfo() function call, it checks that incoming data is command line argument or not. then insert_contact($b) function is call. where i pass array as parameter(here $b ).

What i am trying to do is make my function independent of data(means whatever it is GET,POST or command line argument). I don't want to changes in my function. but make one common solution that support every type of incoming data(POST or command line argument). Following class is working(test with $_POST ),but i am not satisfied with it.

so what can i do so function became independent?
Your suggestion to improve this type of things.
Thanks in advance.

<?php
class validation
{   
    function valid_orginfo()
    {
        $a = array();
        if(isset($argv)) {
            $b = $argv;
        }else {
            $b = $_POST;
        }
        extract($b);
        if($org_name!="" and $add!="")
        {
            $this->insert_contact($b);
        }
    }

    function insert_contact($b)
    {
        global $mysqli;
        extract($b);

        $query = "INSERT INTO `personal`(`first_name`, `last_name`) VALUES ('$sfname', '$slname')";

        $mysqli->query($query) or die($mysqli->error);
        return $mysqli->insert_id;
    }

}
?>

You can fake $_GET and $_POST if it is called from the command line:

if (!isset($_SERVER["HTTP_HOST"])) {
    parse_str($argv[1], $_GET);
    parse_str($argv[1], $_POST);
}

And call like: php script.php 'argument1=x&argument2=y'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM