0

I have created this simple WordPress REST API where users can perform:

  Option A
• Types in the following URL in browser:
      → www.example.com/wp-json/v1/getBook/123456?AuthorSurname=Buckley
   The API will retrieve the relevant information and return a response in the browser:
      → {"Book":"Thank you for smoking"}

  Option B
• Perform a CURL statement in CMD or via scripting:
   (for Example)

function getBook($authorID, $authorSurname){  
    $ch = curl_init();
    $url = 'www.example.com/wp-json/v1/getBook/123456?AuthorSurname=Buckley';
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:application/json','Content-Type:application/json'  ));
    curl_setopt($ch, CURLOPT_URL, $url.$authorID.'?AuthorSurname='.$authorSurname);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //execute the POST request
    $result = curl_exec($ch);       
    return $result;

    curl_close($ch);
<br><br>

I would like to prevent the response from showing whenever user perform Option A.
The response can only be shown via Option B.

How can I achieve this?

5
  • 1
    the purpose of REST is to retrieve data with GET and create or update data with POST and PUT then I don't understand what you want to achieve. edit your question to add more details about the functionality you want.
    – Kaperto
    Commented Dec 23, 2019 at 9:50
  • You mean you want to detect if a user navigates to your REST endpoint and return different content? Your best bet might be to look at the request headers you're getting in both cases and see if you can find something to differentiate them, but I suspect there won't be anything. Why exactly do you need to do this? The REST response can't be secret to your users.
    – Rup
    Commented Dec 24, 2019 at 0:12
  • @Kaperto : My bad for not being specific enough. I have added in more details in my question. Hope this is detailed enough.
    – BBBBB86
    Commented Dec 24, 2019 at 2:19
  • @Rup: I would like to hide the content and allows it to be shown when user uses CURL in CMD or via a script... Is it possible?
    – BBBBB86
    Commented Dec 24, 2019 at 2:20
  • You could test for something a browser won’t set by default e.g. “Accept: application/json” as you’ve got in your script example. But I don’t think there’s a foolproof way to detect browser vs AJAX/curl, no.
    – Rup
    Commented Dec 24, 2019 at 2:25

0

Your Answer

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

Browse other questions tagged or ask your own question.