Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Get PHP or Javascript response from Node.js

$
0
0

I have a node.js program specifically a discord bot using discord.js. I am trying to integrate Googles Teachable Machine API which is just a super easy way of training an image classifier and getting fast results. The problem I am facing is that the API supports javascript but not node.js so I can get results from an html or php file by passing an image link in the header of the url, but I don't know how I can get a result back to my node.js application. I was thinking about creating a php script to echo the data, but I get the data from javascript in the first place and the node.js request functions don't wait for my php script to finish executing before giving a response. So is there a way to wait for a response from a php script? The image classifier takes a few seconds to get a result.

Upon further attempts, I tried to get the script to write to a file and get node.js to just read from the file a few seconds after, but calling the php script via

request.get('php file url', function (error, response, body) {
    if (!error && response.statusCode == 200) {

    }
});

doesnt seem to make the php script do any writing to any files even though when I run it in my browser it does. So does a request to a php file not make it execute the php code?

Shortened version: I have a node.js application which needs data from a php file but the php file uses javascript to generate the data. How can I get the data from the php script?

Edit: Add php code This is basically all my php code. Most of it is javascript which I need to get the data.

$url = htmlspecialchars($_GET["URL"]);
if(empty($url)) {
    $error = "No URL Provided.";
}
$myfile = fopen("../test.txt", "w") or die("Unable to open file!");
fwrite($myfile, $url);
fclose($myfile);

Then I use in my javascript code to get the image url.


Viewing all articles
Browse latest Browse all 138163

Trending Articles