Pregunta

Please help..

I am trying to use Nutritionix API to allow the user to search the database for a food and enter the amount they consumed. My website should then calculate the calories. Once I understand it a little I should be able to continue myself but I am really not sure..on the Nutritionix website they have this example..

curl -XPOST https://api.nutritionix.com/v1_1/search -H 'Content-Type: application/json' -d'
{
 "appId":"APP_ID",
 "appKey":"APP_KEY",
 "query":"Cookies `n Cream"
}
'

I have the APP_ID and APP_KEY. I need to put this code in a php file in some sort of if statement..can someone please give me a quick example of how I would write this into a php file for it to work? At the moment I am literally copying and pasting that into the PHP file to try get some idea and its giving an error at https..any help would be greatly appreciated.

¿Fue útil?

Solución

The example they are giving is for requesting their API using cURL in the command-line. You can try that too.

In PHP you can also use cURL. You could try something like this:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nutritionix.com/v1_1/search");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
        "appId=id&appKey=key&query=milk");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec ($ch);

curl_close ($ch);

print_r($response);
?>

You should google cURL and PHP and read up on that.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top