2
votes

Hi
I am new to FatSecret Platform API and i developed a PHP script to
access food details using foods.search method
Here is the foods.search method Api call example,used Curl and works
perfect.

<?php
$consumer_key = "xxxxxxx";
$secret_key = "xxxxxxx";
//Signature Base String
//<HTTP Method>&<Request URL>&<Normalized Parameters>
$base = rawurlencode("GET")."&";
$base .= "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.
api&";

//sort params by abc....necessary to build a correct unique signature
$params = "method=foods.search&";
$params .= "oauth_consumer_key=$consumer_key&"; // ur consumer key
$params .= "oauth_nonce=123&";
$params .= "oauth_signature_method=HMAC-SHA1&";
$params .= "oauth_timestamp=".time()."&";
$params .= "oauth_version=1.0&";
$params .= "search_expression=".urlencode($_GET['pasVar']);
$params2 = rawurlencode($params);
$base .= $params2;

//encrypt it!
$sig= base64_encode(hash_hmac('sha1', $base, "$secret_key&",
true)); // replace xxx with Consumer Secret

//now get the search results and write them down
$url = "http://platform.fatsecret.com/rest/server.api?".
$params."&oauth_signature=".rawurlencode($sig);

//$food_feed = file_get_contents($url);
list($output,$error,$info) = loadFoods($url);
echo '<pre>';
if($error == 0){
    if($info['http_code'] == '200')
        echo $output;
    else
        die('Status INFO : '.$info['http_code']);

}

else
    die('Status ERROR : '.$error);

function loadFoods($url)
{

        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, $url);

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        $error = curl_error($ch);

        $info = curl_getinfo($ch);
        // close curl resource to free up system resources
        curl_close($ch);

        return array($output,$error,$info);

}

?>

the above script working perfect and retrieves food details using
search method.But

when i use food.get method to retrieve data using food_id it says
invalid signature error,Here's the food.get method api call example,I
have given correct keys and passed food_id parameter.Can someone help
me to solve the issue,I am really struck on this code.It says invalid
signature error.

<?php
$consumer_key = "xxxxxxxxx";
$secret_key = "xxxxxxxxxx";
//Signature Base String
//<HTTP Method>&<Request URL>&<Normalized Parameters>
$base = rawurlencode("GET")."&";
$base .= "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&";

//sort params by abc....necessary to build a correct unique signature
$params = "method=food.get&";
$params .= "oauth_consumer_key=$consumer_key&"; // ur consumer key
$params .= "oauth_nonce=".rand()."&";
$params .= "oauth_signature_method=HMAC-SHA1&";
$params .= "oauth_timestamp=".time()."&";
$params .= "oauth_version=1.0&";
$params .= "food_id=".urlencode($_GET['pasVar']);
$params2 = rawurlencode($params);
$base .= $params2;

//encrypt it!
$sig= base64_encode(hash_hmac('sha1', $base, "$secret_key&",
true)); // replace xxx with Consumer Secret

//now get the search results and write them down
$url = "http://platform.fatsecret.com/rest/server.api?".
$params."&oauth_signature=".rawurlencode($sig);

//$food_feed = file_get_contents($url);
list($output,$error,$info) = loadFoods($url);
echo '<pre>';
if($error == 0){
    if($info['http_code'] == '200')
        echo $output;
    else
        die('Status INFO : '.$info['http_code']);

}

else
    die('Status ERROR : '.$error);

function loadFoods($url)
{

        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, $url);

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        $error = curl_error($ch);

        $info = curl_getinfo($ch);
        // close curl resource to free up system resources
        curl_close($ch);

        return array($output,$error,$info);

}

?>

Help me what's the error in the code and help me to solve the issue as
i have to use this API in my project

Thanks waiting for your replies....

1
I have used your above code but facing the auth signature issue using foods.serach method - deepak bhardwaj

1 Answers

3
votes

You should order you params by alpha. So you should do it in this order:

$params = "food_id=".urlencode($_GET['pasVar'])."&"; 
$params .= "method=food.get&";
$params .= "oauth_consumer_key=$consumer_key&"; // ur consumer key
$params .= "oauth_nonce=".rand()."&";
$params .= "oauth_signature_method=HMAC-SHA1&";
$params .= "oauth_timestamp=".time()."&";
$params .= "oauth_version=1.0";