Python調用PHP的函數

jopen 11年前發布 | 47K 次閱讀 Python Python開發

應用需求:

        在電子商務的web平臺中有可能存在這樣的需求,在月末進行分紅賬務結算,這樣就需要在web服務器下寫腳本定時執行數據庫的操作,這里有很多種可選的方案,Python調

用PHP函數只是其中的一種處理方式。

Python端代碼:

    #!/usr/bin/python  

    import subprocess  

    method="prom_timing_exec"  

    proc=subprocess.Popen(['php -f /var/www/html/vsdev/model/Keke_witkey_prom_timing_exec.php '+method],shell=True,stdout=subprocess.PIPE);  

    response=proc.stdout.read();  

    print(response);  

這里需要注意一點:參數是添加在url后面的,且需要用空格隔開。即使是調用某個函數,其函數名也是需要通過參數傳遞。

PHP端代碼:

    <?php  
       /* 
        * @ author: Houqd 
        * @ date  : 2013-09-06 
        * @ des   : This file contains all function that need timing execute in Python script. 
        **/  
       $method = $argv[1];  

       class db_op  
       {  
            public $_conn;  
            public $_select;  
            public $_dbname;  
            public $_tablename;  
            public $_sql;  
            public $_where;  


            function db_op($dbname)  
            {  
                 $this->_dbname = $dbname;  
                 $this->_tablename = "keke_witkey_prom_event";  
            }  

            function db_connect()  
            {  
                 $this->_conn = @mysql_connect("192.168.1.50","root","dell_456");  
            }  

            function db_select()  
            {  
                 $this->_select = @mysql_select_db($this->_dbname , $this->_conn);  
            }  

            function setWhere($where)  
            {  
                 $this->_where = $where;  
            }  

            function execute($sql)  
            {  
                $result = mysql_query($sql);  
                if($result){  
                      while($row = mysql_fetch_array($result))  
                      {  
                           $return[] = $row;  
                      }  
                      return $return;  
                }  

                return false ;  
            }  


            function query()  
            {  
                 if(!$this->_conn){  
                      $this->db_connect();  
                 }  

                 if(!$this->_select){  
                      $this->db_select();  
                 }  

                 if(isset($this->_where)){  
                       $sql = "select * from %s.".$this->_tablename." where ".$this->_where;  
                 }else{  
                       $sql = "select * from %s.".$this->_tablename;  
                 }  

                 return $this->execute(sprintf($sql , $this->_dbname));  

            }  
       }  

       function prom_timing_exec()  
       {  
           $db_op = new db_op("keke_witkey");  
           $exec_result = $db_op->query();  
           print_r($exec_result);  
       }  

       if(isset($method) && $method != ""){  
            return $method();  
       }else{  
            echo "No function to call.";  
       }  
    ?>  

注意:這里在PHP中接收傳過來的參數是用:$argv變量來接收的,$argv[0]是php文件名,$argv[1]是第一個參數,$argv[2]是第二個參數....

來自:http://blog.csdn.net/houqd2012/article/details/11266319

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!