How to send a variable from processing to be read by PHP script

Hey, I am reading a value from my USB port and store it in a variable (integer) in Processing 2 and I would like to send that variable to a test.php script that will read it and execute functions depending on its value. Is it possible ?

Answers

  • edited June 2015

    Yes. You should use the POST method with the Client library coming with Processing.

    Look at this search for some examples.

  • edited June 2015

    Hello again and thanks for the help PhiLho! I saw an example that creates a Client and writes data into it. When the Client is created I must define the server and port. Well I am using localhost at the moment, 127.0.0.1 apache webserver the path to my php file is something like "http://127.0.0.1/projetoINSMH/teste/showmsg.php"

    which has the following code:

    <?php
    
    $url1=$_SERVER['REQUEST_URI'];
    header("Refresh: 2; URL=$url1");
    
    if (isset($_POST['type'])) {
      $type = $POST['type'];
      echo "got type";
    } else {
      $type = "null";
      echo "nothing received";
    }
    
    if ($type == "ok") {
      echo " correct position ";
    } else if ($type == "error") {
      echo " wrong position ";
    } else {
      echo "This PHP script requires a value";
    }
    
    ?>
    

    I searched for the port of my server and I think it is 3306...

    My processing code:

    import processing.net.*; 
    Client myClient; 
    int clicks=0;
    
    void setup() { 
    
      myClient = new Client(this, "http: //127.0.0.1/projetoINSMH/teste/showmsg.php", 3306); 
    
    }
    
    void draw () {
    
      if(clicks % 2 == 0) {
        myClient.write("ok");
        println("correct position");
      } else {
        myClient.write("error");
        println("wrong position");
      }
    }   
    void mouseReleased() {
      // Count the number of mouse clicks:
      clicks++;
    }
    

    In my processing console it prints the message it is supposed to, but my php script is not receiving anything. I am refreshing the page every 2 seconds.

    If you could shed some light it would be amazing. Thanks again

  • Look at a discussion like http://forum.processing.org/two/discussion/10423/working-with-client-connection-to-api-with-authentication

    The second argument of Client doesn't has a scheme (no http) and no path (nothing after the domain name). You have to specify the header content properly (including the said path, and the HTTP verb), and to build a proper POST payload.

    If that's too hard, and if you have only very simple data to send, you can take the incorrect but convenient way to use loadStrings() (ie. a GET method) with your URL, passing the data as query string (showmsg.php?msg=ok for example).

  • I have tried the loadStrings() with GET method, I replaced the "POST" with "GET" in my php script and this Processing code:

    void setup () {
    
    }
    
    void draw () {
    
      if(clicks % 2 == 0) {
        String [] msg = loadStrings("http://127.0.0.1/projetoINSMH/teste/showmsg.php?type=ok");
        println("ok");
      } else {
        String [] msg = loadStrings("http://127.0.0.1/projetoINSMH/teste/showmsg.php?type=error");
        println("error");
      }
      delay(2000);
    }
    
    void mouseReleased() {
    
     clicks++; 
    }
    

    once again the Processing console is working but nothing happens on the page http://127.0.0.1/projetoINSMH/teste/showmsg.php when I click the mouse and release. It shows the message "nothing received" and after refreshing shows the same. I have also tried this without the "delay()" function. When I type the link with the parameters it shows the correct message, but I would like that to happen when I click with the mouse on the Processing SquarePanel

    thanks and sorry I am new to this software and language

  • _vk_vk
    edited June 2015

    For one thing, in your php script, at line 7, I think you want $_POST instead of $POST.

    I think Your php script always output ""This PHP script requires a value" as $type is never assigned on line 7. Also I'don't know php but I think you might have an scope issue testing $type from line 14 forward.

    Perhaps try first with a very simple script, and add later. for instance, this:

    <?php
    
    
    if (isset($_POST['type'])) {
      $type = $_POST['type'];
      echo "got type. Type is " . $type; 
    } else {
      echo "nothing received\n";
    }
    
    
    ?>
    

    when tested via:

    https://www.hurl.it

    correctly echoed the posted value, 'ok' for instance.

    But I don't know how to properly build the POST request in processing. The POST message is not send in the URL, but in the body of the message which is... i don't really know...

  • I got this response (I passed "bundalele" instead of "ok" as the"type":

    Screen Shot 2015-06-25 at 11.39.24 PM

  • edited June 2015

    Thank you _vk. I noticed that "$POST" typo later. Anyways, I tried to use GET instead since it is not sensitive information and it's a much easier way. I tested my script and it is working properly with the params in the URL. Everything is working Ok in the PHP. But what I want to do is to open this script and show it in a webpage when running the Processing code which I already have:

    int clicks=0;
    
    void setup () {
    
    }
    
    void draw () {
    
      if(clicks % 2 == 0) {
        String [] msg = loadStrings("http://" + "127.0.0.1/projetoINSMH/teste/showmsg.php?type=ok");
        println("ok");
      } else {
        String [] msg = loadStrings("http://" + "127.0.0.1/projetoINSMH/teste/showmsg.php?type=error");
        println("error");
      }
      delay(2000);
    }
    
    void mouseReleased() {
    
     clicks++; 
    }
    

    I was expecting this code to open or at least refresh the param on the page already opened. For example: I open Chrome in http://127.0.0.1/projetoINSMH/teste/showmsg.php and it is showing me the message "nothing received blablabla..." and when I run my Processing code and click on the window to change the value of "clicks" it would refresh the webpage opened on Chrome to OK or ERROR, but I what I see is that loadStrings function is not doing anything :(

  • "I was expecting this code to open or at least refresh the param on the page already opened."
    No, I think you misunderstood how loadStrings() works. No reason for it to interact with a browser.
    Actually, it loads the page and fills the msg array with the resulting lines.
    The side effect is that your PHP code is called each time, so you can transmit your data, per the original request.

Sign In or Register to comment.