# How to build a PHP API Server

**URL:** <https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371>\
**Category:** Hosting Support\
**Created:** [July 21, 2023, 1:57am UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371 "2023-07-21T01:57:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon70622800](https://forum.infinityfree.com/letter_avatar/anon70622800/32/5_5575768a8748004e209b776fc1b2916d.png) [@anon70622800](https://forum.infinityfree.com/u/anon70622800)\
**Post date:** [July 21, 2023, 1:57am UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/1 "2023-07-21T01:57:49Z")

</div>

[### Website URL](http://kutalmisberkejrtest.free.nf/)

Hello,

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

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

---

<div class="post-metadata">

**Author:** ![Meishin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/meishin/32/59902_2.png) [@Meishin](https://forum.infinityfree.com/u/Meishin)\
**Post date:** [July 21, 2023, 2:15am UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/2 "2023-07-21T02:15:17Z")

</div>

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

> [@Ensuring only web browsers can access your website](https://forum.infinityfree.com/t/ensuring-only-web-browsers-can-access-your-website/49353):
>
> InfinityFree is a website hosting service. That means that the hosting accounts we provided are intended for hosting websites. Websites contain pages that are accessed through web browsers. InfinityFree is not intended to be used for file sharing, API hosting, database hosting or background tasks/tools. To help enforce this, free hosting enforces a security system that makes sure that anyone trying to access your website is using a normal web browser. This is done by checking whether the web br…

What we provide is website hosting not application hosting.

> [@anon70622800](#):
>
> but is there anyways to overcome this issue

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.

---

<div class="post-metadata">

**Author:** ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)\
**Post date:** [July 21, 2023, 8:08am UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/3 "2023-07-21T08:08:54Z")

</div>

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](http://www.example.com), the backend must also be on [www.example.com](http://www.example.com). Hosting the backend on, say, [api.example.com](http://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.

> [@anon70622800](#):
>
> Also, If I use different parameters and methods (like URL\_Name/products/display), I receive:
> 
> 500 Internal Server Error #### Something on the website crashed!

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.

---

<div class="post-metadata">

**Author:** ![anon70622800](https://forum.infinityfree.com/letter_avatar/anon70622800/32/5_5575768a8748004e209b776fc1b2916d.png) [@anon70622800](https://forum.infinityfree.com/u/anon70622800)\
**Post date:** [July 21, 2023, 9:42am UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/4 "2023-07-21T09:42:10Z")

</div>

Thank you for your response.

> [@Admin](#):
>
> Our hosting only allows the GET and POST HTTP methods. Other methods, such as PUT, PATCH and DELETE, are blocked

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?

---

<div class="post-metadata">

**Author:** ![SpookyKipper](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/spookykipper/32/27461_2.png) [@SpookyKipper](https://forum.infinityfree.com/u/SpookyKipper)\
**Post date:** [July 21, 2023, 10:04am UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/5 "2023-07-21T10:04:46Z")

</div>

> [@anon70622800](#):
>
> No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

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

> **[Access-Control-Allow-Origin - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin)**
>
> The Access-Control-Allow-Origin response header indicates whether the response can be shared with requesting code from the given origin.

* * *

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

---

<div class="post-metadata">

**Author:** ![anon70622800](https://forum.infinityfree.com/letter_avatar/anon70622800/32/5_5575768a8748004e209b776fc1b2916d.png) [@anon70622800](https://forum.infinityfree.com/u/anon70622800)\
**Post date:** [July 21, 2023, 10:11am UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/6 "2023-07-21T10:11:47Z")

</div>

> [@SpookyKipper](#):
>
> `htaccess` file with the following content:  
> `Header set Access-Control-All`

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?

---

<div class="post-metadata">

**Author:** ![KangJL](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/kangjl/32/12130_2.png) [@KangJL](https://forum.infinityfree.com/u/KangJL)\
**Post date:** [July 21, 2023, 10:22am UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/7 "2023-07-21T10:22:24Z")

</div>

> [@SpookyKipper](#):
>
> You can add this header by creating a `.htaccess` file with the following content:  
> `Header set Access-Control-Allow-Origin "example.com"`

Will not work on free hosting

---

<div class="post-metadata">

**Author:** ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)\
**Post date:** [July 21, 2023, 12:08pm UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/8 "2023-07-21T12:08:36Z")

</div>

> [@anon70622800](#):
>
> Are methods in question blocked on premium as well?

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.

---

<div class="post-metadata">

**Author:** ![system](https://infinityfree-forum-uploads.s3.dualstack.eu-central-1.amazonaws.com/original/3X/b/0/b024fd3fa88bae093c9576e55c2c94e33f890206.svg) [@system](https://forum.infinityfree.com/u/system)\
**Post date:** [July 28, 2023, 12:08pm UTC](https://forum.infinityfree.com/t/how-to-build-a-php-api-server/79371/9 "2023-07-28T12:08:41Z")

</div>

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