Running PHP script via processing

edited February 2017 in Library Questions

Hi!

I have this wordpress plugin that creates and verifies new software licenses. It does this by sending an array of data to the server. I'm trying to write a piece of processing code that creates a license key. I can successfully send variables to my PHP script and I know they are received; I check this using an if statement in my PHP code that checks if the variable 'send' is received and sends the array of data I mentioned before:

Processing code:

import processing.net.*; 

Client c; 

String secret_key = "56fc633286bc72.62466890";
String action = "slm_create_new";
String f = "James";
String l = "Doe";
String email = "jdoe@yahoo.com";    

void setup() { 
size(200, 200); 
background(50); 
fill(200);
c = new Client(this, "www.roham.comxa.com", 21);  

loadStrings("http://"+"roham.comxa.com/test.php/?key="+secret_key);
loadStrings("http://"+"roham.comxa.com/test.php/?action="+action);
loadStrings("http://"+"roham.comxa.com/test.php/?first="+f);
loadStrings("http://"+"roham.comxa.com/test.php/?last="+l);
loadStrings("http://"+"roham.comxa.com/test.php/?email="+email);
String[] b = loadStrings("http://"+"roham.comxa.com/test.php/?send=GO");
println(b);
} 

void draw() {  
   if (c.available() > 0) {
    String dataIn = c.readString();
    println(dataIn);
  }
}

PHP script:

<?

$postURL = "roham.comxa.com"; 

$data=array();
$data['secret_key']=isset($_GET['key'])?$_GET['key']:"";
$data['slm_action']=isset($_GET['action'])?$_GET['action']:"";
$data['first_name']=isset($_GET['first'])?$_GET['first']:"";
$data['last_name']=isset($_GET['last'])?$_GET['last']:""; 
$data['email']=isset($_GET['email'])?$_GET['email']:"";


$PHPSubmit = isset($_GET['send'])?$_GET['send']:"";

if($PHPSubmit == "GO")
{
        echo "Sending Command ";  
    $ch = curl_init ($postURL); 
    curl_setopt ($ch, CURLOPT_POST, true); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
    $returnValue = curl_exec ($ch);
    var_dump($returnValue);
    echo $returnValue;
}
?>

So by logic, if the script enters the if statement, I should be able to successfully create a new license, but I only get "Sending Command" and "string(25631)" which means there was no reply and no license is created when I check the database. I understand that this may be a PHP issue but want to make sure that the processing code is correct. Please help if you know anything about this!

Thanks!

Answers

  • edited April 2016

    It has been awhile since I did any PHP but it seems to me that the script test.php is going to be called 6 times and each time it will set one element of the array but what happens to the other 5. It would appear that it sets the other elements to an empty string.

    For instance the PS line 17 will set the 'key', the next line, 18, will set the 'action' but will also clear the 'key'. By the time you set 'sen' all the other values are empty strings.

    Of course I could be wrong :)

  • You are absolutely right! I tried moving the 'send' string around and it stopped going into the if statement. I'm not sure how to load an entire array at once though. Any ideas?

  • edited April 2016

    It should be possible to concantenate the query string so that you only have to make one http request. Something like this, hopefully ;)

    String script = "http://"+"roham.comxa.com/test.php/?");
    String q = "key="+secret_key;
    q += "&action="+action;
    q += "&first="+first;
    q += "&last="+last;
    q += "&email="+email;
    q += "&send=GO";
    String[] b = loadStrings(script + q);
    

    The & is used to separate parameters in the query stirng.

  • & is the traditional query parameter separator

  • I have changed the code above to use the &. Thanks for that its been a long time since I did any PHP.

    To convert the query string into an associative array try the PHP parse_str function. Google should give you examples of its use. :)

  • Thank you guys, really appreciate the help!

  • Hey guys so I'm back on this problem again, tried the above code, the request is being sent but I get a bunch of HTML back instead of the Json response I'm supposed to be getting, am I supposed to read the answer differently?

  • I am not an expert of PHP but you should move lines 5 to 10 to inside your conditional statement of your php code. The reason is that you are sending a single request with all the information. In your php code, when you have a GO token, it means you should be able to retrieve the other tokens as well (key, action, first, etc.) This should ensure your $data array is properly set with their corresponding values. This is similar to what @quark mention in its first post.

    Kf

Sign In or Register to comment.