0

I need to load a url and have the only text on this page be a JSON string.

As an example I built a simple function to get the day of the week: getDayOfWeek(). My actual function is much more complex, and cannot easily be converted to another language.

But I cannot simply load this into the DOM. I need the only text on the page to be the JSON object.

For example:

http://example.com/what_day_is_it // needs to return: { "day": "Friday" }

Here's a fiddle: http://jsfiddle.net/7b49Lorw/

HTML:

<span id="day"></span>

JS:

function getDayOfWeek() {
  var d = new Date();
  d.getDay();
  var gsDayNames = new Array(
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday'
  );
  return gsDayNames[d.getDay()];
}
$('#day').text(getDayOfWeek());

Assuming I use PHP for the server side, how can I use PHP to evaluate the result of a JS function and return the JSON string?

Ryan
  • 14,682
  • 32
  • 106
  • 179
  • Do you mean seralizing getDayOfWeek to JSON? – Matías Fidemraizer Apr 03 '15 at 16:59
  • I need to be able to have another app call this web page: http://example.com/what_day_is_it and have the only text on that page be `{ "day" : "Friday" }` – Ryan Apr 03 '15 at 17:07
  • I don't understand the issue at all but I'll try to address it with an incoming answer. Tell me if I got your issue rightly – Matías Fidemraizer Apr 03 '15 at 17:08
  • Are you trying to create a rest service that returns current day name? – sbjumani Apr 03 '15 at 17:08
  • see http://stackoverflow.com/questions/10472927/add-content-to-a-new-open-window – madalinivascu Apr 03 '15 at 17:08
  • You can't do it using a HTML page and JavaScript. You will need some backend scripting like PHP. Otherwise when a app sends a request to this url, the JavaScript won't execute. – Hoyen Apr 03 '15 at 17:10
  • Ah I see, I didn't see you wanted to send it from the server. I thought you had it done already... – Matías Fidemraizer Apr 03 '15 at 17:12
  • I'm fine using PHP to render the page. I just need help figuring that out. – Ryan Apr 03 '15 at 17:15
  • @Ryan If that function is really difficult to convert to another language,you could have a look at [Nodejs](https://nodejs.org/) . – phpcoderx Apr 03 '15 at 17:18
  • Your JavaScript code is client side, so whatever calls your http://example.com/what_day_is_it URL will have to evaluate the code. If you want to run that code server side, you have to have a server with a server side installed JS engine. Alternatively, convert the code to PHP. – Gordon Apr 03 '15 at 17:20

3 Answers3

0

Since you included a PHP tag with your question (even though you didn't mention it):

<?php 
echo json_encode(array('day' => date('l')));

edit: Since we can only guess at what you're actually trying to do, and since you feel the difficulty of translating your 1000+ line JS function to some other language isn't worth it, why don't you just wrap the output of your function with something like:

JSON.stringify({ "day": <YourFunctionThatReturnsTheDay> });

and display it when the DOM is ready? This would be assuming that whatever client that calls your page has the ability to parse and run Javascript.

WWW
  • 9,734
  • 1
  • 29
  • 33
  • Right, but my actual function is 1000 lines or so of javascript to calculate a simple string. Not easily converted into PHP. Though I'm open to using PHP if necessary to fetch and build the JSON string without any html. – Ryan Apr 03 '15 at 17:12
0

In php, you can generate code based on some arguments. For example you can have a paramter in you GET request, like: http://example.com/what_day_is_it?onlyJSON=true

And then in your code, wrap the visual part in

<!-- Your JSON print goes here -->
<?php if ($_GET['onlyJSON']){ ?>
<span id="day-of-week">

</span>

<!-- Your HTML code without parameter goes here-->
<?php } else {  ?>

<span> MY BIG PAGE </span>

<? } ?>

and your javascript will be that:

function getDayOfWeek() {
    var d = new Date();
    d.getDay();
    var gsDayNames = new Array(
      'Sunday',
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday'
    );
    return gsDayNames[d.getDay()];
}
$('#day').text(getDayOfWeek());
$('#day-of-week').html(JSON.stringify({day: getDayOfWeek()}));

That will make the page display only your JSON. From the text perspective it will be that. However, the page itself (its source) will not contain only that json. It will have HTML code + Javascript that processes it. To have a function in JS you need it to be a part of the Page.

  • I need to call this from another app that is expecting a pure JSON object. Think of it like a simple API call. – Ryan Apr 03 '15 at 17:23
  • That's what I thought. In this case you need a server to generate JSON instead of HTML web-page with JS function. So you have 2 options. First is to rewrite the funciton in PHP. If you don't want to go away from JS, change your server from php to NodeJS. It's javascript and you can Ctrl+C -> Ctrl+V 90% of your code in your server logic. – Alexander Troshchenko Apr 03 '15 at 17:25
0

You could install v8js (a PHP extension which allows the interpretation of Javascript code inside PHP). This guide covers installing and get started with v8js:

http://css.dzone.com/articles/running-javascript-inside-php

thodic
  • 2,229
  • 1
  • 19
  • 35