PHP中API调用返回的JSON中未定义的索引
这是我执行HTTP请求的PHP文件:PHP中API调用返回的JSON中未定义的索引,php,json,api,undefined-index,Php,Json,Api,Undefined Index,这是我执行HTTP请求的PHP文件: $token='Food2forkKey'//食物叉 Foo2Work私钥 $fooddetail=新食品($token,“json”); $pid=$_GET['first']; //$search='1https://community-food2fork.p.mashape.com/search?key=“.$token.&q=”.$pid//食物叉 $search='1http://food2fork.com/api/search?key=“.$t
$token='Food2forkKey'//食物叉
Foo2Work私钥
$fooddetail=新食品($token,“json”);
$pid=$_GET['first'];
//$search='1https://community-food2fork.p.mashape.com/search?key=“.$token.&q=”.$pid//食物叉
$search='1http://food2fork.com/api/search?key=“.$token.&q=”.$pid;
//钥匙
echo$搜索;
$json=$fooddetail->调用url($search);
//详细信息;
如果(!$json){
echo“错误:无法检索产品列表”;
退出();
}
//var_dump(json_decode($json,TRUE));
$details=json_decode($json,TRUE);
//var_dump(详细信息);
详细信息;
$title=$details['title'];
//打印$标题;
$publisher=$details['publisher'];
//印刷费$出版商;
$image=$details['source_url'];
//打印r$图像;
?>
"
这是Html文件
无标题文件
脚本失败,出现“undefine index”错误消息。原因是什么
我使用food2fork API。返回的JSON有
title
和publisher
键,但当我尝试显示这些值时,脚本会记录上述错误消息。共享$JSON
值以及为什么在img src中出现打印($image);
?您能显示打印($details)吗值?您应该在使用前检查密钥是否实际存在,例如:if(isset($details['publisher']){…}
它已解决用户搜索时,我只希望显示五个产品
$token='Food2forkKey'; //food2fork
foo2fork private key
$fooddetail=new Food($token,"json");
$pid=$_GET['first'];
//$search='https://community-food2fork.p.mashape.com/search?key='.$token.'&q='.$pid; //Food2fork
$search='http://food2fork.com/api/search?key='.$token.'&q='.$pid;
//key
echo $search;
$json=$fooddetail->call_url($search);
//echo $details;
if(!$json){
echo 'Error: Could not retrieve products list.';
exit();
}
//var_dump(json_decode($json,TRUE));
$details=json_decode($json,TRUE);
//var_dump($details);
echo $details;
$title = $details['title'];
//print_r $title;
$publisher = $details['publisher'];
//print_r $publisher;
$image=$details['source_url'];
//print_r $image;
?>
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<td><?php print_r ($title); ?></td>
<td><img src="<?php print_r($image); ?>"</td>
</tr>
</table>
</body>
</html>
This is Html file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="get" action="foodsearch.php">
<input type="text" name="Search" />
<input type="submit" value="Submit" id="sub" />
</form>
</body>
</html>