- Home /
Is it possible to call a Javascript code on local server from Unity? (Google Chart API involved)
I am testing different ideas, so this is the problem I ran into.
I have two php scripts on my local server.
The first one does the following: It connects to a database and takes all of the values I need - dates and sales. After it connects to a database, I use Google Chart Api (I use standard JavaScript to call it) to create a chart with the values I got. Once the chart is created, I save the chart as a png (with the help of the second php file, which converts a base64 data string to an image) to my DB.
The problem I encountered is that I actually have no idea how to make this script run. I tried to use the WWW function;
string query = linkToGetImageOfStats_str;
WWW result_WWW = new WWW (query);
yield return result_WWW;
but the only thing I would get is the actual code of that php file I am calling, without actually saving the image to my database, meaning - the JS code doesn't not get executed, even though it's part of that php-file. But when I actually run that script in the browser, everything is fine.
Any ideas? Thanks.
Answer by Bunny83 · Jan 08, 2016 at 01:37 PM
That sounds like you have a wrong view of what code is executed where. PHP is a "preprocessor language". It's ment to be included in an HTML document. The PHP interpreter recognises the PHP tags in the source file, executest some actions and removes those tags and might add something else at this place. In the end the source file is sent with all those modifications to the client.
Javascript inside a HTML document is never executed on the server. Those scripts are ment to be executed on the client by the internet browser how is able to read HTML, render that content and execute Javascript code inside those HTML files.
If you want your server to contact that google API you have to look for a PHP solution, maybe using cURL.
I never used that Chart API from Google but it seems there is a PHP implementation. So forget about Javascript.
Answer by phil_me_up · Jan 07, 2016 at 09:57 PM
It sounds like your server isn't setup correctly if you're getting the php script contents returned. Are you using Apache? What happens if you visit that link in a browser?
Assuming your server is setup correctly, then you have the right idea. You make a WWW call to the address of your scripts and include any values you want to submit to that script as part of the query string or as a POST.
I am using XA$$anonymous$$PP.
Everything is fine then. I visit that link, php code gets the needed values, then I make a call to Google Charts Api with JavaScript. I think that's precisely the problem. When I use the WWW class, it gets the whole content of the page without executed JS code. It would be fine, if it was executed - I would stll be able to get the lines I need, but... So, what happens is that Javascript part of the script doesn't not get executed. This code just gets returned as a text, where I would get the html tags and javascript lines.What happens if you visit that link in a browser?