I have a device that sends python http request to a server. I want to make a server side php response script that collect some data from a database and send back to the device in the form of json. I made the python http request. the scrip contains a json message (information about the device) how to make the server php to response with data from database (it should identify the device from http request that holds IP). I need to send some configuration parameters(maximumCurrent, maximumVoltage) they are in a database (configDB) with table name (Parameters) with columns deviceIP, maximumCurrent, maximumVoltage. Any suggestions. (I know php, but dont't know how to make it run automatically based on the http request. because I am planning to send more http request. so php script should response based on different requests )

import requests
import urllib3
import json
import os
import time
import sys

#http Reques

url = 'http://someurl_to_server'
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}

while True:    
    postMessage = '{"Info": {"deviceIP": {"IP" :12345}}}'    
    response = requests.post(url, json=postMessage, headers=headers)   

        decodedresponse = json.loads(response.text)

    except(ValueError, KeyError, TypeError):       
        print("JSON Error")

1 Answers

stefo91 On

Very basic variant:

    $link = mysqli_connect("localhost", "my_user", "my_password", "my_db");

    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;

    if (mysqli_query($link, "INSERT INTO your_table('your_field') VALUES('".json_decode($_POST['json'])."')") {
        echo json_encode("Insert successfully.\n");
        echo json_encode($_POST);