我正在尝试使用Fatsecret REST API返回的数组中的food_id和food_name创建一个下拉列表,但没有成功。
我的代码是
<?php
$base = rawurlencode("GET")."&";
$base .= "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&";
$params = "format=json&";
$params = "method=foods.search&";
$params .= "oauth_consumer_key=my key&";
$params .= "oauth_nonce=123&";
$params .= "oauth_signature_method=HMAC-SHA1&";
$params .= "oauth_timestamp=".time()."&";
$params .= "oauth_version=1.0&";
$params .= "search_expression=flour";
$params2 = rawurlencode($params);
$base .= $params2;
//encrypt it!
$sig= base64_encode(hash_hmac('sha1', $base, "4e0b6b00578b4a8995975b289879ae4e&", true));
$url = "http://platform.fatsecret.com/rest/server.api?".$params."&oauth_signature=".rawurlencode($sig);
返回的数组对象是
object(SimpleXMLElement)#555 (4) {
["max_results"]=> string(2) "20"
["total_results"]=> string(3) "186"
["page_number"]=> string(1) "0"
["food"]=> array(20) {
[0]=> object(SimpleXMLElement)#549 (5) {
["food_id"]=> string(4) "3419"
["food_name"]=> string(11) "White Flour"
["food_type"]=> string(7) "Generic"
["food_url"]=> string(63) "http://www.fatsecret.com/calories-nutrition/generic/flour-white"
["food_description"]=> string(75) "Per 100g - Calories: 364kcal | Fat: 0.98g | Carbs: 76.31g | Protein: 10.33g" }
[1]=> object(SimpleXMLElement)#554 (6) {
["food_id"]=> string(7) "1727595"
["food_name"]=> string(17) "All-Purpose Flour"
["brand_name"]=> string(10) "Gold Medal"
["food_type"]=> string(5) "Brand"
["food_url"]=> string(72) "http://www.fatsecret.com/calories-nutrition/gold-medal/all-purpose-flour"
["food_description"]=> string(77) "Per 1/4 cup - Calories: 100kcal | Fat: 0.00g | Carbs: 22.00g | Protein: 3.00g" }
[2]=> object(SimpleXMLElement)#553 (6) {
["food_id"]=> string(5) "56342"
["food_name"]=> string(17) "All Purpose Flour"
["brand_name"]=> string(7) "Wegmans"
["food_type"]=> string(5) "Brand"
["food_url"]=> string(69) "http://www.fatsecret.com/calories-nutrition/wegmans/all-purpose-flour"
["food_description"]=> string(78) "Per 1/4 cup - Calories: 100kcal | Fat: 0.00g | Carbs: 22.00g | Protein: 4.00g" }
此时我尝试了以下代码
echo '<select>';
$jsonData = file_get_contents($url);
$jsonDataObject = json_decode($jsonData);
foreach($jsonDataObject->response->values as $option){
echo '<option value=' . $option->food_id . '>' . $option->food_name . '</option>';
}
echo '</select>';
但没有运气。
最终我尝试了
$food_feed = file_get_contents($url);
$food = simplexml_load_string($food_feed);
$obj_xml = simplexml_load_string($food_feed);
// Total number of elements present ///
$total = $obj_xml->count(); // total number of elements for PHP 5.3 and above
$str="<select name='student'>";
for($i=0; $i<$total; $i++) {
$str= $str . "<option value=".$obj_xml->food[$i]->food_id.">".$obj_xml->food[$i]->food_name. "</option>";
}
$str = $str. "</select>";
echo $str;
此代码确实有效,但在下拉列表末尾添加了一些额外的空行,我无法找到原因。
如果有人能帮助我找到更好的方法来实现目标,我将不胜感激。
此致
答案 0 :(得分:1)
好的,我已经设法解决这个问题,以防万一这可以帮助别人。
$ food将是一个simplexml对象,我们可以访问像
这样的元素foreach( $food->{'[array]'} as $array_variable )
所以我们的循环将变得像
$food = simplexml_load_string($food_feed);
//print_r($food);
if ($food->total_results != "0") {
foreach( $food->{'food'} as $food_entry ) {
echo $food_entry->food_id. "<br />\n";
echo $food_entry->food_name. "<br />\n";
echo $food_entry->food_description. "<br />\n";
echo "<br />\n";
}
}
else { //do some thing else
}