简体   繁体   中英

Sending data using POST from PHP to raspberry's Python script

i recently found the way to send data from a python file to a php file using request post and now i would like to be able to send data always with the same method of a php file to a python file knowing that my file php is in a server and the python file is in a raspberry that is connected to the 3g Internet, but I do not know too much about this domain i am still new i show you my code to send a python file to a PHP file :

raspberry python client :

import requests
import time
i =1
j= 0
while i==1 :
   j = j+1
   userdata = {"id": j ,  "firstname": "jo", "lastname": "rasp",
               "password": "666"}
   resp = requests.post('http://jawad.meswatts.fr/sous/api.php',
                          data = userdata)
   print(resp.text)
   time.sleep(3)

php server script :

<?php
try
    {
      $bdd = new PDO('mysql:host=localhost;dbname=jawad;charset=utf8',
                     'jawad', 'knlnklml54vn,6');
    }
   catch(Exception $e)
   {
     die('Erreur : '.$e->getMessage());
   }


      $id = htmlspecialchars($_POST["id"]);
      $firstname = htmlspecialchars($_POST["firstname"]);
      $lastname = htmlspecialchars($_POST["lastname"]);
      $password = htmlspecialchars($_POST["password"]);


      $req = $bdd->prepare( '                            
                            INSERT INTO reception(nom,prenom,identifiant,id)
                            VALUES (:lastname,:firstname,:password,:id);
                            ' );

    $req->execute(array('lastname' => $lastname, 'firstname' => 
                       $firstname,'password' => $password,'id' => $id)); 
  ?>

So here are my two programs that work very well now I would like to do that in the other way I know PHP very well but I'm not very very strong network can you help me please ?

So the problem with your current plan is that you must be able to POST to somewhere.

In the case of the pi posting to the server, we are posting to http://jawad.meswatts.fr/sous/api.php

But if the server wanted to post to the pi. Where would it post to? The pi isn't publicly available at any domain name or public IP. (Its possible to set it up this way, but I wouldn't recommend it for beginners).

Instead I would start with how everyone else has solved this in the past. Have the pi make a GET request to another location on the server. This function would return the information that would have been posted. In this way, the server never tries to find the pi. It just waits until it is told to store some information (POST) or until it is asked what some information is (GET).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM