How to build a PHP API Server

### Website URL

Hello,

I have never used a hosting service before, so my questions might be a little weird, sorry in advance :slight_smile:

I am trying to build a PHP API server which will have a React frontend. It’s an entry level task so I’m not allowed to use any frameworks and such for backend, therefore I had to use my react client as a separate view component which sends ajax(axios) requests to the server (POST,GET and DELETE, I’ve designed it as a RESTful api)

Anyways, I managed to host the backend but I have a couple of issues so far. I can’t get or post data using POSTMAN and my frontend application. It throws CORS errors, which I’ve had before but managed to fix them in localhost. But I got no idea what to do in here.

Error Message

Access to XMLHttpRequest at ‘URL_Name’ from origin ‘LOCALHOST’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I’ve read that this is intended to a degree, but is there anyways to overcome this issue? As I said, I fixed the issue on localhost but I’m not really sure how to approach in here. Also, If I use different parameters and methods (like URL_Name/products/display), I receive:

500 Internal Server Error #### Something on the website crashed!

Any help would be greatly appreciated. Thanks

Hi there,
Building APIs violates the our ToS, and is thus blocked by this:

What we provide is website hosting not application hosting.

If you really need to build APIs, then please consider upgrading to premium hosting.
Another possible apporach is to use a custom domain with Cloudflare, which will bypass the security system, but I’ve never tested it, and doing it here still violates the ToS.

6 Likes

You can build a Single Page Application with a PHP API here, but there are some restrictions. The most notable ones are:

  • Our hosting blocks CORS requests. This means the frontend and the backend must be hosted on the same hostname. If the backend is on www.example.com, the backend must also be on www.example.com. Hosting the backend on, say, api.example.com, or testing the frontend locally, will result in the requests getting block.
  • Our hosting only allows the GET and POST HTTP methods. Other methods, such as PUT, PATCH and DELETE, are blocked.

A 500 error is usually the result of a PHP code crash. If changing API parameters causes this, I’m pretty sure that your PHP code is just unable to handle those parameters.

8 Likes

Thank you for your response.

DELETE and PUT method being blocked is a bummer, I need to find an alternative in this case then. I can debug other errors but I need a way to some make alterations for my list items etc… Are methods in question blocked on premium as well?

1 Like

You will just need to add the header Access-Control-Allow-Origin?


You can add this header by creating a .htaccess file with the following content:
Header set Access-Control-Allow-Origin "example.com"

2 Likes

Thank you, I know that but I tend to listen to owner who says CORS requests are blocked, and hosting a React frontend and a PHP backend under same name doesn’t seem intuitive because of different enviroments, what do you think?

Will not work on free hosting

6 Likes

No, they are not blocked on premium. Both the CORS restrictions and HTTP method restrictions are only in effect on free hosting and do not apply to premium hosting.

6 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.