简体   繁体   中英

how to run a python file using exec() function in php

this my php form (form1.php)

<html>
<body>

<form action="process1.php" method="post">
E-mail: <input type="text" name="email"  required ><br><br>
Password: <input type="text" name="pass"  required ><br><br>
<input type="submit">
</form>
        <html>
    <body>

process.php

    <?php $email = $_POST["email"]; ?>
    <?php $pass  = $_POST["pass"]; ?>
    
    
    <?php echo($email) ?>    <br>
    <?php echo($pass) ?>   <br> 
   

i want to use $email, $pass variable values from php. And i want to send data from the varibles to my salesforce account using python to a custom object...

python file

import requests
import json
from simple_salesforce import Salesforce

sf = Salesforce(username="USER", password= "PWD",security_token="" )
print(sf)
params = {
    "grant_type": "password",
    "client_id": "X0000XXXX", 
    "client_secret": "XX00000X",
    "username": "XXX0XX", 
    "password": "XX0XX"
}

dataset = [{'email__c':'$email(this should be the variable from php)', 'password__c': '$pass (this should be the variable from php) '}]
sf.bulk.aryan__c.insert(dataset,batch_size=1,use_serial=True)
    
   

First you need to make your python file CLI compatible. You need to catch the arguments . The easiest way to do it would be something like this:

import sys

def myFunc(user, pwd):
    print(user,pwd)

if __name__ == '__main__':
    
    if len(sys.argv) >= 3:
        user = sys.argv[1]
        pwd = sys.argv[2]
        myFunc(user, pwd)

Also have a look atargparse and click .

Then you can call the script from PHP and provide the credentials. There are actually several ways to do it. While your wish exec will not return the output of the python script

passthru

$output = passthru("python3 myScript.py $user $pwd");

shell_exec

$prog = 'python3 myScript.py '.$user.' '.$pwd;
$output = shell_exec($prog);

exec

exec("python3 myScript.py $user $pwd");  

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