How to make PHP function restful web service?










-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.










share|improve this question
























  • Can any one give me an example of rest api in php and where I should start.

    – user9320601
    Nov 16 '18 at 5:51











  • Rest has nothing to do with the functionality, but only how you map requests to the functionality. Recommend looking into slimframework.com for a low overhead implementation.

    – user1032531
    Nov 16 '18 at 5: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 7:08















-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.










share|improve this question
























  • Can any one give me an example of rest api in php and where I should start.

    – user9320601
    Nov 16 '18 at 5:51











  • Rest has nothing to do with the functionality, but only how you map requests to the functionality. Recommend looking into slimframework.com for a low overhead implementation.

    – user1032531
    Nov 16 '18 at 5: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 7:08













-1












-1








-1


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.










share|improve this question
















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.







php ajax rest api restful-url






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 7:24

























asked Nov 16 '18 at 5:30







user9320601



















  • Can any one give me an example of rest api in php and where I should start.

    – user9320601
    Nov 16 '18 at 5:51











  • Rest has nothing to do with the functionality, but only how you map requests to the functionality. Recommend looking into slimframework.com for a low overhead implementation.

    – user1032531
    Nov 16 '18 at 5: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 7:08

















  • Can any one give me an example of rest api in php and where I should start.

    – user9320601
    Nov 16 '18 at 5:51











  • Rest has nothing to do with the functionality, but only how you map requests to the functionality. Recommend looking into slimframework.com for a low overhead implementation.

    – user1032531
    Nov 16 '18 at 5: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 7:08
















Can any one give me an example of rest api in php and where I should start.

– user9320601
Nov 16 '18 at 5:51





Can any one give me an example of rest api in php and where I should start.

– user9320601
Nov 16 '18 at 5:51













Rest has nothing to do with the functionality, but only how you map requests to the functionality. Recommend looking into slimframework.com for a low overhead implementation.

– user1032531
Nov 16 '18 at 5:52






Rest has nothing to do with the functionality, but only how you map requests to the functionality. Recommend looking into slimframework.com for a low overhead implementation.

– user1032531
Nov 16 '18 at 5: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 7:08





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 7:08












1 Answer
1






active

oldest

votes


















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






share|improve this answer

























  • 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.

    – user9320601
    Nov 16 '18 at 7:23











  • just add this on the top of your API (PHP). header("Access-Control-Allow-Origin: *");

    – Sayed Mohd Ali
    Nov 16 '18 at 7:24











  • ok how can I use POST method and how can I call from jquery now I am using GET method.

    – user9320601
    Nov 16 '18 at 7: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 7: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 7:37











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331971%2fhow-to-make-php-function-restful-web-service%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer

























  • 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.

    – user9320601
    Nov 16 '18 at 7:23











  • just add this on the top of your API (PHP). header("Access-Control-Allow-Origin: *");

    – Sayed Mohd Ali
    Nov 16 '18 at 7:24











  • ok how can I use POST method and how can I call from jquery now I am using GET method.

    – user9320601
    Nov 16 '18 at 7: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 7: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 7:37















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






share|improve this answer

























  • 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.

    – user9320601
    Nov 16 '18 at 7:23











  • just add this on the top of your API (PHP). header("Access-Control-Allow-Origin: *");

    – Sayed Mohd Ali
    Nov 16 '18 at 7:24











  • ok how can I use POST method and how can I call from jquery now I am using GET method.

    – user9320601
    Nov 16 '18 at 7: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 7: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 7:37













0












0








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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 7:31

























answered Nov 16 '18 at 6:14









Sayed Mohd AliSayed Mohd Ali

1,5302520




1,5302520












  • 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.

    – user9320601
    Nov 16 '18 at 7:23











  • just add this on the top of your API (PHP). header("Access-Control-Allow-Origin: *");

    – Sayed Mohd Ali
    Nov 16 '18 at 7:24











  • ok how can I use POST method and how can I call from jquery now I am using GET method.

    – user9320601
    Nov 16 '18 at 7: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 7: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 7:37

















  • 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.

    – user9320601
    Nov 16 '18 at 7:23











  • just add this on the top of your API (PHP). header("Access-Control-Allow-Origin: *");

    – Sayed Mohd Ali
    Nov 16 '18 at 7:24











  • ok how can I use POST method and how can I call from jquery now I am using GET method.

    – user9320601
    Nov 16 '18 at 7: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 7: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 7:37
















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.

– user9320601
Nov 16 '18 at 7:23





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.

– user9320601
Nov 16 '18 at 7:23













just add this on the top of your API (PHP). header("Access-Control-Allow-Origin: *");

– Sayed Mohd Ali
Nov 16 '18 at 7:24





just add this on the top of your API (PHP). header("Access-Control-Allow-Origin: *");

– Sayed Mohd Ali
Nov 16 '18 at 7:24













ok how can I use POST method and how can I call from jquery now I am using GET method.

– user9320601
Nov 16 '18 at 7:27





ok how can I use POST method and how can I call from jquery now I am using GET method.

– user9320601
Nov 16 '18 at 7: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 7:35






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 7: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 7:37





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 7:37



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331971%2fhow-to-make-php-function-restful-web-service%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin