· 6 years ago · Oct 20, 2019, 04:22 PM
1<?php
2//Search query by GET method as srch
3$_GET['search'] = "8f6b372eaf684bd58b3eb6def6d4ad9e";
4$consumer_key = "03c556c90ac44fd98ca4a2cab8520987";
5$secret_key = "666efd5086c547a2a3a4cec762704d2c";
6$base = rawurlencode("GET")."&";
7$base .= "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&";
8//sort params by abc....necessary to build a correct unique signature
9$params = "format=xml&";
10$params .= "method=foods.get_recently_eaten&";
11$params .= "oauth_consumer_key=".$consumer_key."&"; // ur consumer key
12$params .= "oauth_nonce=".rand()."&";
13$params .= "oauth_signature_method=HMAC-SHA1&";
14$params .= "oauth_timestamp=".time()."&";
15$params .= "oauth_token=".urlencode($_GET['search'])."&";
16$params .= "oauth_version=1.0";
17
18$params2 = rawurlencode($params);
19$base .= $params2;
20$sig= base64_encode(hash_hmac('sha1', $base, $secret_key."&",
21 true)); // replace xxx with Consumer Secret
22$url = "http://platform.fatsecret.com/rest/server.api?".
23 $params."&oauth_signature=".rawurlencode($sig);
24//echo $url;
25list($output,$error,$info) = loadFoods($url);
26echo '<pre>';
27if($error == 0){
28 if($info['http_code'] == '200')
29 echo $output;
30 else
31 die('Status INFO : '.$info['http_code']);
32}
33else
34 die('Status ERROR : '.$error);
35function loadFoods($url)
36{
37 // create curl resource
38 $ch = curl_init();
39 // set url
40 curl_setopt($ch, CURLOPT_URL, $url);
41 //return the transfer as a string
42 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
43 // $output contains the output string
44 $output = curl_exec($ch);
45 $error = curl_error($ch);
46 $info = curl_getinfo($ch);
47 // close curl resource to free up system resources
48 curl_close($ch);
49 return array($output,$error,$info);
50}
51?>