2

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")
~~~
3
  • First tell us POST or GET request? If request is post recive in PHP with $_POST if GET recive with $_GET Commented May 28, 2019 at 8:10
  • it is POST. 'response = requests.post(url, json=postMessage, headers=headers) ' Commented May 28, 2019 at 8:11
  • Ok then in your php file recive with this: <?php json_decode($_POST['json']) then make insert query in database. Commented May 28, 2019 at 8:12

1 Answer 1

3

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.\n");
    }
    else{
        echo json_encode($_POST);
    }
    mysqli_close($link);
}
Sign up to request clarification or add additional context in comments.

7 Comments

than you so much. I will try it and get back ;)
This was really helpful. Thank you. One more addition. what if the request was GET. How to send some data back to the device ?
If request is GET you can send data back with echo json_encode('your data here', JSON_UNESCAPED_UNICODE); P.S. click 'acceptance' if works :)
somehow it doesn't work!. response is just a plain html file. contains nothing other than <html> and <body> tags. I tried just an integer inside as echo to check if there is any response. no output. status code is 200!
I made changes try again and check your $_POST structure.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.