Recipe - Food - Nutrition

FREEMIUM
Verified
By David | Updated 13 days ago | Food
Popularity

9.9 / 10

Latency

485ms

Service Level

100%

Health Check

N/A

Back to All Discussions

Beginner to writing API code

Rapid account: No 1 Uknow
No1uknow
8 years ago

I know this sounds horrible but I am brand new to writing application code. I do not understand how / where to use the “Get/Post” code and where to use the “request example”. I was planning on using Java (I thought it would be the easiest). Can you point me in the right direction of how to get started. I just need a complete example of where to put each piece of code and I should be able to take it from there.

I can script I am just brand spanking new to creating this type of code.

What goes in the HTML, javascript file(script.js)(assuming this is needed), where should I put the include for unirest.io snippet etc.

I was looking at this as an example “https://www.smashingmagazine.com/2012/02/beginners-guide-jquery-based-json-api-clients/” but it doesn’t seem to carry over to your API usage.

Thank you very much for any help provided.

Rapid account: Spoonacular
spoonacular Commented 8 years ago

I’m currently on vacation and these are also more PHP related questions than really related to the API so I think you can get quicker more in-depth help in a PHP forum. If you don’t get ahead, I’ll be back in May, let me know 😉

Rapid account: No 1 Uknow
No1uknow Commented 8 years ago

json_decode gives an error and says it wants to use json_encode. After getting the result from json_encode I cannot figure out how to parse it (foreach)? json_encode seems to convert the data to a string and is not easily loopable.
response=Unirest\Request::get("response = Unirest\Request::get("url", array( “X-Mashape-Key” => “” ) );
echo "What type of var1: " . gettype(response);
That above line of code returns a type “string”.

Ideas?

Rapid account: Spoonacular
spoonacular Commented 8 years ago

…well there you go 😃 You need to get the “raw_body”, parse it as json (see PHP json_decode) and then you can grab the “answer” field from that

Rapid account: No 1 Uknow
No1uknow Commented 8 years ago

object(Unirest\Response)#1 (4) { [“code”]=> int(200) [“raw_body”]=> string(202) "{“answer”:“In 2 apples, there are 16.74 mg of Vitamin C. This amount covers 20% of your daily needs of Vitamin C.”,“image”:"https://spoonacular.com/cdn/ingredients_100x100/apple.jpg",“type”:“NUTRITION”}" [“body”]=> object(stdClass)#2 (3) { [“answer”]=> string(102) “In 2 apples, there are 16.74 mg of Vitamin C. This amount covers 20% of your daily needs of Vitamin C.” [“image”]=> string(57) “https://spoonacular.com/cdn/ingredients_100x100/apple.jpg” [“type”]=> string(9) “NUTRITION” } [“headers”]=> array(16) { [0]=> string(15) “HTTP/1.1 200 OK” [“Access-Control-Allow-Headers”]=> string(1) “" [“Access-Control-Allow-Methods”]=> string(39) “: GET, HEAD, POST, OPTIONS, DELETE, PUT” [“Access-Control-Allow-Origin”]=> string(1) "” [“Allow-Control-Allow-Methods”]=> string(39) “: GET, HEAD, POST, OPTIONS, DELETE, PUT” [“Allow-Control-Allow-Origin”]=> string(1) “*” [“Content-Encoding”]=> string(4) “gzip” [“Content-Type”]=> string(16) “application/json” [“Date”]=> string(29) “Sun, 10 Apr 2016 18:29:31 GMT” [“Server”]=> string(13) “Mashape/5.0.6” [“X-RateLimit-classifications-Limit”]=> string(2) “25” [“X-RateLimit-classifications-Remaining”]=> string(2) “-7” [“X-RateLimit-requests-Limit”]=> string(4) “1000” [“X-RateLimit-requests-Remaining”]=> string(3) “968” [“Content-Length”]=> string(3) “180” [“Connection”]=> string(10) “keep-alive” } }

Rapid account: Spoonacular
spoonacular Commented 8 years ago

please try var_dump($response) to see what you actually get back

Rapid account: No 1 Uknow
No1uknow Commented 8 years ago

I am getting the following error on the line "echo $response[“answer”];…

Fatal error: Cannot use object of type Unirest\Response as array

Rapid account: Spoonacular
spoonacular Commented 8 years ago

try var_dump($response) to see what you actually get back

Rapid account: No 1 Uknow
No1uknow Commented 8 years ago

<?php
require_once ‘/unirest-php/src/Unirest.php’;
$response = Unirest\Request::get(“https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/quickAnswer?q=How+much+vitamin+c+is+in+2+apples%3F”, array( “X-Mashape-Key” => MY KEY" ) )
echo $response[“answer”];?>

I had the require_once in the header 1st then just tried to add it in the body and it still gives the same result.

Rapid account: No 1 Uknow
No1uknow Commented 8 years ago

So I finally got Unirest installed on server and tried your code above and it doesn’t seem to be working. It prints out “MY KEY” echo $response[“answer”];?>

Here is the code I am using:

<html>
<head>
<?php
require_once ‘`/unirest-php/src/Unirest.php’;
?>

</head>

<body>
<?php

$response = Unirest\Request::get(“https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/quickAnswer?q=How+much+vitamin+c+is+in+2+apples%3F”, array( “X-Mashape-Key” => “MY KEY” ) )
echo $response[“answer”];?>
</body>

</html>

Hmmmm How do you post code blocks? Its removing all the html tags.

Rapid account: Spoonacular
spoonacular Commented 8 years ago

Of course, build on 😃

Join in the discussion - add comment below:

Login / Signup to post new comments