如何使用从数据库到python http请求的一些数据进行php服务器脚本响应

如何使用从数据库到python http请求的一些数据进行php服务器脚本响应

问题描述:

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)   

    try:      
        decodedresponse = json.loads(response.text)

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

我有一个设备将python http请求发送到服务器。 我想制作一个服务器端的php响应脚本,它从数据库中收集一些数据并以json的形式发送回设备。 我做了python http请求。 该脚本包含一个json消息(有关该设备的信息)如何使服务器php响应来自数据库的数据(它应该从保存IP的http请求中识别设备)。 我需要发送一些配置参数(maximumCurrent,maximumVoltage),它们位于数据库(configDB)中,表名为(参数),列为deviceIP,maximumCurrent,maximumVoltage。 任何建议。 (我知道php,但是不知道怎么让它根据http请求自动运行。因为我打算发送更多的http请求。所以php脚本应该根据不同的请求做出响应) p >

  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'} 
 
,True:
 postMessage ='{“Info”:{“deviceIP”:{“IP”:12345  } 
'响应= requests.post(url,json = postMessage,headers = headers)
 
尝试:
 decoderesponse = json.loads(response.text)
 
除外(ValueError,KeyError,  TypeError):
 print(“JSON Error”)
 ~~~ 
  code>  pre> 
  div>

Very basic variant:

<?php 
if(!empty($_POST['json'])){
    $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;
        exit;
    }

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