Skip to content
Advertisement

How to get and send data from python selenium to php

Excuse my English please. my system is designed by php and is about connecting together with Amazon auto ordering system(designed by python with selenium)

Here’s the scenario: Customer orders the product on my website -> send data to Amazon auto ordering system(designed by python with selenium) -> place the order what customer ordered with automation system -> send data to my system(php)

I am wondering how to send data to python and get data from python I think send data with Json format to python and get data with Json format from python would work

Here’s python code python code

Here’s temporary php code for sending data to python

function curlPostForOrder($url, $params){
    $ch = curl_init();
        
    curl_setopt($ch, CURLOPT_URL , $url);
    
    curl_setopt($ch, CURLOPT_USERPWD, $params['client_token'].":".""); 
        
    $JsonData = json_encode($params['data']);               //Json data post
    curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonData);        //Set Post Data ;
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                //Set ReturnTransfer; 
    curl_setopt($ch, CURLOPT_POST, 1);                          //Set Post value true ( regular HTTP POST )
        
    $headers = array();
    $headers[] = "Content-Type: application/x-www-form-urlencoded";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
    $retValue['status'] =true;
    
    $ret= curl_exec($ch);
    error_log($ret);
    if ($ret==false) {
        echo 'Error: Curl Error';
        $retValue['status'] = false;
        $retValue['error_msg'] = curl_error($ch);
    }else{
        $retValue['data'] = json_decode($ret,true);
    }
    curl_close($ch);
        
    return $retValue;

Here’s temporary php code for get data from python

function curlPostForRetrieve($url, $params){
    $ch = curl_init();
        
    curl_setopt($ch, CURLOPT_URL , $url);
    curl_setopt($ch, CURLOPT_USERPWD, $params['client_token'].":".""); //Set UserPWD;
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                //Set ReturnTransfer; 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    
    $retValue['status'] =true;
        
    $ret=curl_exec($ch);
    
    if ($ret==false) {
        echo 'Error: Curl Error';
        $retValue['status'] = false;
        $retValue['error_msg'] = curl_error($ch);
    }else{
        $retValue['data'] = json_decode($ret,true);
    }
    curl_close ($ch);
        
    return $retValue;

Thank you for your opinions

Advertisement

Answer

In python you can write a simple program to read a JSON file and then write the results out. These are the two functions for doing that:

import json

def read_json(file_path:str) -> dict:
    """Takes in a json file path and returns it's contents"""
    with open(file_path, "r") as json_file:
        content = json.load(json_file)
    return content

def store_json(data:dict, file_path:str):
    """Takes in a python dict and stores it as a .json file"""
    with open(file_path, "w") as json_file:
        json.dump(data, json_file)

read_json() takes a file path and returns the python dict with the data. You can then do any necessary preprocessing and feed that into the automation script (amz.py). If you need to store the result, take the result from the amz.py and store to a JSON file using store_json() which takes the data as the first param and file path and just writes the data to the file path.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement