oauth_signature problem in php

80 views
Skip to first unread message

Neeraj Malik

unread,
Mar 2, 2022, 5:44:17 AMMar 2
to FatSecret Platform API
Hi everyone,

I am facing oauth_signature problem in php code.I send you the source code for this as following below:-

 <?php

$consumer_key = "****";
$secret_key = "****";

$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=recipe.add_favorite&";
$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 .= "oauth_token=****&";
$params .= "recipe_id=7042366&";


$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);
// echo $url;
//$food_feed = fle_get_contents($url);
list($output, $error, $info) = loadFoods($url);

if ($error == 0) {
    if ($info['http_code'] == '200') {

    echo "<pre>";
        print_r($output);
        echo "</pre>";
       
    } 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);
   
    $xml = simplexml_load_string($output);
    $json = json_encode($xml);
    $array = json_decode($json,TRUE);
//    return array($array,$error,$info);
    return array($array,$error,$info);
}

?>

Show this output:-
Array
(
 [code] => 1
 [message] => An unknown error occurred: please try again later
 )

Please send the complete solution for this so that i will get proper output with details.
Reply all
Reply to author
Forward
0 new messages