PHP中

时间:2017-01-25 05:26:41

标签: php json api undefined-index

这是我执行HTTP请求的PHP文件:

    $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>

脚本失败并显示“undefine index”错误消息。这背后的原因是什么?

我使用 food2fork API。返回的JSON具有titlepublisher个键,但是当我尝试显示值时,脚本会记录上述错误消息。

0 个答案:

没有答案