HIT POT
HIT POT

Reputation: 13

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)
    
   

Upvotes: 1

Views: 270

Answers (1)

user_na
user_na

Reputation: 2273

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 at argparse 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");  

Upvotes: 1

Related Questions