0

I have a small script on my server to catch a POST request, like this:

<?php

$jsonformat = json_encode($_POST);

$jsonFile = fopen("data.json", "w") or die("Unable to open file!");
fwrite($jsonFile, $jsonformat);
fclose($jsonFile);
echo "success";

?>

Above script is named index.php and placed in http://example.com/data/report/

I have another script for sending the POST request like this:

<?php

$url = 'http://example.com/data/report/';

$data = array('key1' => 'value1', 'key2' => 'value2');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

?>

The two script are working great. But in another situation the $url is like this: 'http://example.com/data/report' Notice no "/" at the end of the url. In the situation without "/" at the end the json-file on server is still generated but it is empty.

I have no possibility adding the "/" to the url. Its a device where I can not edit the code but I know it is missing "/" at the end so code above is just my example. I have reproduced the issue, I think.

So my question is: - Why is it working with "/" but not without? - Can I do anything on the server so http://example.com/data/report will be the same as http://example.com/data/report/

I have access to .htaccess on the server

miken32
  • 42,008
  • 16
  • 111
  • 154
droid
  • 103
  • 1
  • 15

1 Answers1

0

Have you got a .htaccess file or anything that is rewriting your URLs?

For example I have a line in mine like:

RewriteRule ^([^\.]+)$ $1.php [L,NC,QSA]

This removes the file extension from the browser URL bar. So if I go to mywebsite.com/mydirectory/myfile it will automatically presume it means mywebsite.com/mydirectory/myfile.php.

If I went to mywebsite.com/mydirectory it would try to rewrite this as mywebsite.com/mydirectory.php unless I put the / at the end so it knows it's a directory.

Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76
  • Hi. No rewriting in .htaccess – droid May 28 '19 at 15:44
  • Open up the network tab in your console and then as you test it you should see the requests being sent over the network, if you click on them it gives more details, this might give you some indication as to what is happening, or at least more information that you can share here to try figure it out – Paddy Hallihan May 28 '19 at 15:49
  • Yeah I presume the request URL it gives is the same as you hard coded in the example above so you've no way to determine if it is treating it as a file or a directory. I think something is making it treat it like a file unless you add the `/` if not necessarily the `.htaccess`. I presume you want to have some sort of dynamic URL and that's why you can't just use the hard coded one? – Paddy Hallihan May 28 '19 at 16:03
  • Its a device where I can put into my hostname. I put in http://www.example.com and device will then make a POST request to http://www.example.com/data/report but I want it to contact http://www.example.com/data/report/ I have been in contact my manufacture and they refuse to add / to the URL – droid May 28 '19 at 16:09
  • That's all I can think of at the moment unfortunately – Paddy Hallihan May 28 '19 at 16:19
  • I will have a second look at what has been written, maybe I did it wrong. Thanks for replies :) – droid May 28 '19 at 16:50