1

I made a simple API with PHP

When I call it (Postman or script) with https://www.example.org/api/index_api.php it works. I get the return value.

When I call it (Postman or script) with https://www.example.org/api/ it doesn't.

Object sent

{
  "id": "alberto",
  "name": "test name naaame ",
  "email": "[email protected]"
}

index_api.php (simplified: here, I don't use $method):

<?php
header('Content-type: application/json; charset=utf-8');

$method = $_SERVER['REQUEST_METHOD'];
//
$input = json_decode(file_get_contents('php://input'), true);
$input = mb_convert_encoding($input, 'HTML-ENTITIES', "UTF-8");
//
echo json_encode(['3' => 'De vuelta de API : ' . $input['name'] ]);

I'd be really grateful to receive some help.

I tried a lot of URL rewrite rules in .htaccess, but no success.

When it's for myself, I use complete path but it's not an API. And I need to call it

  • for clients
  • from inside a WP plugin

Thanks by advance

7
  • Please provide your webserver configuration. It seems you need to set up the index file correctly in the webserver configuration, but I'm not sure until I see it. Commented Dec 4, 2024 at 19:36
  • @Mikhail Chuloshnikov : Thanks, you are totally right: I've just installed my scripts on a MAMP, and it works. The server on which i made previous tests is a VPS with Plesk. I'll dig into this direction Thanks a lot Commented Dec 4, 2024 at 20:01
  • What happens when index_api.php is renamed to index.php? It works with ../api/index_api.php because you specify which file should be used, I think that when using ../api/ it tries to look for an index.php file, which obviously does not exist. Commented Dec 5, 2024 at 12:42
  • @geertjanknapen : Thanks for suggesting I've tried using index.php. Whatever the name of the php file I specify in the htacess, same result. After reinstalling my server without Plesk, all is working perfectly. The problem comes from Plesk or my lack of knowledge of how it works. Thanks to the people who helped me, I think my request was badly formulated. The title should have been : Htaccess problem of a specific directory in conflict with general redirection rules on a Plesk (Obsidian) / Ubuntu (22.04) Commented Dec 7, 2024 at 13:04
  • @LenaLfPC You could try setting a default index type in Plesk Obsidian. I don't have experience with Plesk, but that could do the trick. Commented Dec 9, 2024 at 7:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.