将数据返回到网页

I have am trying to build a website in which I can track my fitness and nutrition.

I would like to use the API that is available from USDA http://ndb.nal.usda.gov/ndb/doc/apilist/API-FOOD-REPORT.md

and this is where I want to get to

http://nutritiondata.self.com/facts/cereal-grains-and-pasta/5680/2

not identical but similar, but with ongoing tracking and being able to record the food I have eaten to my own database.

Appreciating there are apps out there that offer this functionality (myplate) for example I really fancy the challenge of doing this kind of thing myself.

I have setup a Joomla site, I have checked that CURL is available and active I've installed Sorcerer by NoNumber I've read loads of articles on the construction of curl events.

How do I return data to my page.
The closest (I think that I have got is with)

$ch = curl -H "Content-Type:application/json" -d '{"ndbno":"01009","type":"f"}' DEMO_KEY@api.nal.usda.gov/ndb/reports;

$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch); curl_close($ch); fclose($fp);

This brings my site offline with the error Parse error: syntax error, unexpected `'"Content-Type:application/json' (T_CONSTANT_ENCAPSED_STRING) in /mounted-storage/home147/sub036/sc85544-PGMS/Domain/plugins/system/sourcerer/helper.php(570) : runtime-created function on line 8

Fatal error: Function name must be a string in /mounted-storage/home147/sub036/sc85544-PGMS/Domain/plugins/system/sourcerer/helper.php on line 575`

When using Sorcerer, you may want to edit your content in WYSIWYG mode. Your sytax will look something like:

{source}
<?php
$ch = curl -H "Content-Type:application/json" -d '{"ndbno":"01009","type":"f"}' DEMO_KEY@api.nal.usda.gov/ndb/reports;
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch); curl_close($ch);
fclose($fp);
{/source}

Note that if you do this, your "source code" will most likely be URL-encoded. I mention this because I suspect that the single or double-quotes might be interfering with content parsing. This could be remediated by writing your code and encapsulating it (with {source}...{/source} in wysiwyg mode. This is just a guess, though.

Another option is that if all you need to do is display the contents of a JSON API response, you may want to use jQuery Ajax.

Example:

I will first show you sample jQuery Ajax syntax followed by the html for the div container that can display the output. Note that the headers line is optional (depends on the requirements of the API).

jQuery(document).ready(function(){
        var requestUrl= "https://www.annatech.com/api/v1/content/single/31";
        jQuery.ajax({
            url: requestUrl,
            type: "GET",
            headers: { 'token': '9YZi+i7tPpLedg9LtFcuu8rUL3eiCnYuGnNH:650' },
            success: function (resultData) {
                jQuery( "#output" ).append(resultData.article.introtext).html;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('error');
            },

            timeout: 120000
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1>Test AJAX page load</h1>
<div id="output"></div>

The above example uses the cAPI Joomla Rest API, http://getcapi.org. Disclaimer: I developed it.

As you can see, it is possible to query a RESTful JSON API and output the contents directly on a web page (all client-side, in browser). The example above even passes a token through the header to authenticate access to a restricted page.

</div>