-1

I am new to rest API. I have this below function which returns HTML data I want this to be accessed from anywhere. I am using Zend with PHP 5.6 please give your suggestion.

 fheader('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");

if ($_SERVER['REQUEST_METHOD'] == "GET") {
    $db = MysqliDb::getInstance();
    //$sq = "ct-head";
    $db->where("slug","ct-head");
    $db->orderBy('version', 'desc');
    $template = $db->getOne("tbl_templates");
    $result = $template['modhtml'];

    echo $result;

} else if ($_SERVER['REQUEST_METHOD'] == "POST") {
        echo "POST";
} else {
        http_response_code(405);
}

I have written something like this but if I access this from other system it is returning CORS error.

Any help would be appreciated.

  • Can any one give me an example of rest api in php and where I should start. –  Nov 16 '18 at 05:51
  • Rest has nothing to do with the functionality, but only how you map requests to the functionality. Recommend looking into https://www.slimframework.com/ for a low overhead implementation. – user1032531 Nov 16 '18 at 05:52
  • Rest API means it doesn't have a state. In your case you need to access a function from anywhere, that function may return a template or json data. You should make it available in a url ie, by calling that url your server must execute this function and return result. For example if you are calling www.mydomain.com/api/my_function you need to get the result. Configure in that way. Don't forget to implement any kind of authentication mechanism in between. – Golwin Nov 16 '18 at 07:08

1 Answers1

0

Bro, you need to understand what is rest API first and what purpose you need it for. Also, MySQL is insecure you should switch to PDO or MySQLi.

Rest API is nothing special it is just an AJAX request. we send JSON data to our rest API and on our API(PHP) side we put some condition on that data to make our API work. If you want to send JSON data from anywhere(different servers, POSTMAN etc) You have to use CORS(CROSS-ORIGIN RESOURCE SHARING) for it. so that your server will allow you to accept data from anywhere. You also have to make and authentication key(Access token). so that you only receive data from trusted sites with a valid access token.

SIMPLE EXAMPLE WITH DEMO

I told in my answer you need cross-origin resource sharing. To told server about this, we use some code to let it know that we are allowing CRUD operations from request outside of our own server. sometimes we only make restful API for ourselves Because of security purpose no server will allow anyone to hit on our site.

use this code at the top of your restful API to allow the request from anywhere outside of your server.

header("Access-Control-Allow-Origin: *");

read more about CORS here

Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28
  • gone through this example explained nicely also i have modified my question. If I access it from other system it is thowing error called CORS. –  Nov 16 '18 at 07:23
  • just add this on the top of your API (PHP). header("Access-Control-Allow-Origin: *"); – Sayed Mohd Ali Nov 16 '18 at 07:24
  • ok how can I use POST method and how can I call from jquery now I am using GET method. –  Nov 16 '18 at 07:27
  • please update your API and jQuery in new question if you are having an issue with it. simply if you are sending post request from ajax than you need to use $_GET['POST'] on PHP to receive data. and if you are using GET method of AJAX then you neeed to use $_GET on your API. – Sayed Mohd Ali Nov 16 '18 at 07:35
  • accept this answer and make a new question if you are having some issues with your code. thanks – Sayed Mohd Ali Nov 16 '18 at 07:37