How to use the api in PHP (for Wordpress)

340 views
Skip to first unread message

Jeremy Timessen

unread,
Oct 21, 2012, 5:03:17 PM10/21/12
to fatsecret-p...@googlegroups.com
Hi,

I've tried for quite a few hours to implement the Fatsecret api with PHP. I haven't succeeded yet.
That's why I came here. Does someone have a basic implementation of lets say.. the foods.search function?
There's no need for deep integration. My plan is to make a sidebar widget where people can search for a food item.
Does anyone have an example?

Thank you for your time and help!

Kristina A.

unread,
Apr 17, 2013, 6:30:05 PM4/17/13
to fatsecret-p...@googlegroups.com
I'm about to embark on a very similar project, Jeremy, and wondered if you'd had any luck or response on this that you might be willing to share before I begin. :)

Thanks!

Billy

unread,
May 19, 2013, 1:56:07 PM5/19/13
to fatsecret-p...@googlegroups.com
Jeremy and Kristina

Try this:

<?php 
$consumer_key = "123456789101112"; 
$secret_key = "123456789101112"; 
//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=apple"; 
$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); 

?>

Leah Branch

unread,
Jul 23, 2014, 12:31:07 PM7/23/14
to fatsecret-p...@googlegroups.com
Billy,
Would you be willing to help me install this on my WP site? I have attempted this for months and don't know where to begin. I'm extremely teachable and would like to get this up and running on my site quickly. Thoughts?

Leah

Braunson

unread,
Sep 14, 2014, 1:12:33 PM9/14/14
to fatsecret-p...@googlegroups.com
Create a new custom template in WordPress and use the code from Billy in that custom template, then for whatever page you want to use it on, select that custom template (in WordPress's Admin area).
Reply all
Reply to author
Forward
0 new messages