前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python调用PHP的函数

Python调用PHP的函数

作者头像
py3study
发布2020-01-07 16:10:58
3.4K0
发布2020-01-07 16:10:58
举报
文章被收录于专栏:python3

应用需求:

        在电子商务的web平台中有可能存在这样的需求,在月末进行分红账务结算,这样就需要在web服务器下写脚本定时执行数据库的操作,这里有很多种可选的方案,Python调

用PHP函数只是其中的一种处理方式。

Python端代码:

代码语言:javascript
复制
#!/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端代码:

代码语言:javascript
复制
<?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]是第二个参数....

好久没有写文章了,坚持每天的学习。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档