POSTing to php file returns 405

75 views
Skip to first unread message

Ryan Norman

unread,
Apr 10, 2023, 9:35:25 AM4/10/23
to dotCMS User Group
I'm attempting to POST to (and have run) a PHP file saved on my dotCMS site. The POST is coming from an Arduino program (reports temperature). The PHP script is super simple ... pasted it here:

"<?php
$time - time();
$tempF = $_POST["temp"];
$file = 'dashboard.html';
$data = $time." - ".$tempF;
file_put_contents($file, $data);
?>"

The Arduino can connect to my site and find the PHP file. However, it returns a 405 error which I'm assuming is because dotCMS is not allowing the execution of the PHP code.

Any insights from anyone would be hugely appreciated!

Ryan Norman

unread,
Apr 17, 2023, 2:16:35 PM4/17/23
to dotCMS User Group
Pinging this one again. Does anyone out there have any advice?

Mark Pitely

unread,
Apr 17, 2023, 2:42:16 PM4/17/23
to dot...@googlegroups.com
dotCMS is not normally configured to run PHP on the same server. It can be done, but it is not a standard thing; the file system is also not usually directly accessible (writing a file).  
The correct dotCMS implementation here would be to either use the API to create a piece of database content (using the POST from your Arduino), or use a webhook. 
I'm not saying you can't use PHP, but I am indicating the reason your question isn't getting any answers - there are a 1000 php-based CMS engines and dotCMS is usually differentiated or chosen because it is Java based.

I actually do a lot of PHP-based integrations with dotCMS, but I use a different server to execute the PHP and then push the contents using one of the tools.
It wouldn't hurt for you to tell us which version you are using, as well, as the rules change quite a bit. 

Mark Pitely
Albright College

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/60b1daaa-04a9-429f-a9f0-f0d5ea350494n%40googlegroups.com.

Ryan Norman

unread,
Apr 17, 2023, 3:52:27 PM4/17/23
to dotCMS User Group
I am using Version: 22.09 (9/13/2022).

Setting up another server is likely out of the question as I'm looking to save some cash. However, technically, I believe the ESP8266 that I'm using as my Arduino is its own web server. This web server creates itself when the board is fired up and connects to my wifi. It then reaches out to my PHP file (or it's supposed to) which in turn should generate an HTML page. However, it makes total sense that the file system isn't accessible, pages are technically DB content correct?

Do you have any examples of using "the API to create a piece of database content... or [using] a webhook?" I can probably figure out from there how to build what I need to on the Arduino side of things (maybe...).

Thanks!

Ryan O'Dell

unread,
Apr 18, 2023, 3:49:38 AM4/18/23
to dotCMS User Group
It would be possible to implement a Java plugin that interprets the PHP script.

I did a quick search and our favourite site had some answers: https://stackoverflow.com/questions/12897070/how-can-i-run-php-code-within-a-java-application

I'm not going to duplicate the post here, but I'm sure you get the idea.

Hope this helps.
Ryan

Mark Pitely

unread,
Apr 18, 2023, 1:04:10 PM4/18/23
to dot...@googlegroups.com
Ryan,
If you look at this page:
And skip down to Example 3 you can get the idea (I hope). You can basically create an API endpoint to do whatever you want. Granted this would imply your adruino can make a JSON call (easiest) although you can write code that will get any POST or GET variables, and then you can save the request as content. This means writing Velocity to do what your PHP was doing. I bet you can search the dotCMS listserv for discussion of this remote webhook. 

Example 3: Accept a remote webhook using the scripting API

This example uses the scripting API to accept a webhook POST and sends out the recieved data as an email. To use this, create a .vtl file under /application/apivtl/text/post.vtl This file will handle POSTs to the endpoint /api/vtl/text. As you can see, the folder name /text/ is the name of the endpoint. If you need to handle PUTs or GETs as well, you can add files called ./put.vtl or ./get.vtl respectively. Below is the contents of the ./post.vtl file. It accepts the json data in a variable called $bodyMap and iterates over it to build the email message. It then sends this message out using the $mailer tool included in dotCMS. Learn more about Scripting API.

M



Reply all
Reply to author
Forward
0 new messages