Hello!,
Just wondering if I'm able create a switch script in javascript that sends a request to a
webserver.(i.e. xmlhttp.open("GET / POST","myurl.com/something.php",true)); if not is there
any way for a script to interact with a webserver (i.e. Database connection, pull back
object data, etc)
any help would be amazing!
Thanks!
JS Switch Script to Webserver
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
JS Switch Script to Webserver
Switch only has e.download() function. Most people are using the open source cURL program to implement web services in Switch.
JS Switch Script to Webserver
Thank you for the fast response dkelly!
So in essences the only way to communicate with a webserver through switch is using their web services? I cannot create a switch script do accomplish this? or even create a database connection to pull information. Thanks in advance!
EDIT:
what about installing a wamp / mamp server locally? and access a php file, has anyone had any success with this?
So in essences the only way to communicate with a webserver through switch is using their web services? I cannot create a switch script do accomplish this? or even create a database connection to pull information. Thanks in advance!
EDIT:
what about installing a wamp / mamp server locally? and access a php file, has anyone had any success with this?
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
JS Switch Script to Webserver
no, Switch Web Services module will not help you as it's only for connecting __to__ Switch. Not from Switch to a remote webservice.
There are several examples on this forum using cURL to connect to webserices from a Switch script. Do a quick search.
There are several examples on this forum using cURL to connect to webserices from a Switch script. Do a quick search.
JS Switch Script to Webserver
I am sorry but searched for cURL on here and didn't really find anything helpful. would you happen to have an example of using cURL to hit a webpage and bring the object created back? It would be much appreciated. Thank you for the help so far dkelly!
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
JS Switch Script to Webserver
Here's the basics
var XML = "<orders><order id="1234"/></orders>";
var argc = 0;
var argv = new Array;
argv[argc++] = "curl";
argv[argc++] = "-i";
argv[argc++] = "-H";
argv[argc++] = "Content-type: text/xml";
argv[argc++] = "-X";
argv[argc++] = "POST";
argv[argc++] = "http://myurl.com/something.php";
argv[argc++] = "-d";
argv[argc++] = XML;
var exitCode = Process.execute(argv);
if (exitCode == 0) {
var xmlResponse = Process.stdout;
} else {
job.fail("REST call failed");
}
You can get cURL binaries on Mac OS X using 'port' or 'homebrew'. On Windows I recommend cygwin distribution.
-Dwight Kelly
var XML = "<orders><order id="1234"/></orders>";
var argc = 0;
var argv = new Array;
argv[argc++] = "curl";
argv[argc++] = "-i";
argv[argc++] = "-H";
argv[argc++] = "Content-type: text/xml";
argv[argc++] = "-X";
argv[argc++] = "POST";
argv[argc++] = "http://myurl.com/something.php";
argv[argc++] = "-d";
argv[argc++] = XML;
var exitCode = Process.execute(argv);
if (exitCode == 0) {
var xmlResponse = Process.stdout;
} else {
job.fail("REST call failed");
}
You can get cURL binaries on Mac OS X using 'port' or 'homebrew'. On Windows I recommend cygwin distribution.
-Dwight Kelly
JS Switch Script to Webserver
Awesome dkelly, Thank you so much!!!
JS Switch Script to Webserver
I am sure you will have a solution with Dwight's script, but for the sake of completeness let me add that you can use MAMP to run PHP scripts in Switch as well. In that case you use "Execute command" where the command is to run PHP and the parameter is the path to the PHP script and everything else you need. What you do in the PHP script (upload stuff, fill in forms, ...) that is of course up to the script.
If you feel more comfortable in a PHP script than in the manipulation of a cURL command line in a Javascript, you have an alternative.
Freddy
If you feel more comfortable in a PHP script than in the manipulation of a cURL command line in a Javascript, you have an alternative.
Freddy
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
JS Switch Script to Webserver
MAMP is pretty heavy weight for just PHP scripting. Just install PHP using macport/homebrew or cygwin.
JS Switch Script to Webserver
@freddyp
if i would put a mamp server on our mac so switch could talk to it via "execute command" would i be able to send objects between the two or would it be xml formats like the cURL solution? Also would you have an example of using the execute function to hit a file, then bring back either an object or xml
@dkelly
Yeah i agree that mamp server is heavy, so the would be a better solution
Thank you guys, really appreciate it!
if i would put a mamp server on our mac so switch could talk to it via "execute command" would i be able to send objects between the two or would it be xml formats like the cURL solution? Also would you have an example of using the execute function to hit a file, then bring back either an object or xml
@dkelly
Yeah i agree that mamp server is heavy, so the would be a better solution
Thank you guys, really appreciate it!
JS Switch Script to Webserver
As you would be running it as a CLI the way to talk to the php script is to pass parameters and the way to talk back is to create a file.
I do not have a ready-made example, but here is what you should do. In Execute command you call the php server in "Command or path". In "Arguments" you write the path to the php script and "%1" for the input file and "%2" for the output file. That output file does not exist yet of course, that is the one you will be creating. Use "%3" for a folder in case you will be writing multiple files. And use "File at path" in combination with %2 and "Result in folder" in combination with %3 in "Output".
You can also call a shell script/bat file in "Command or path" and call php from there. I find that cleaner.
In the php script you use $argv[1], $argv[2], ... for the parameters you are passing. And that is it. If you only passed "%2" as a parameter to the php script, then this script
would put the HTML source code of the Google start page into a file in the output folder of your Execute command.
Freddy
I do not have a ready-made example, but here is what you should do. In Execute command you call the php server in "Command or path". In "Arguments" you write the path to the php script and "%1" for the input file and "%2" for the output file. That output file does not exist yet of course, that is the one you will be creating. Use "%3" for a folder in case you will be writing multiple files. And use "File at path" in combination with %2 and "Result in folder" in combination with %3 in "Output".
You can also call a shell script/bat file in "Command or path" and call php from there. I find that cleaner.
In the php script you use $argv[1], $argv[2], ... for the parameters you are passing. And that is it. If you only passed "%2" as a parameter to the php script, then this script
would put the HTML source code of the Google start page into a file in the output folder of your Execute command.
Freddy