为了了解REST,我正在学习/复制一个教程。"$_get"是空白的,我注意到被调用的URL是空白的。这里是一个副本,
http://localhost/RestClient/index.php?action=get_user&id=
但是我点击的href对我来说还可以。
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
这是我的代码,我是PHP的新手,所以我会边做边计算!!!!
<?php
/*** this is the client ***/
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the get parameter action is get_user and if the id is set, call the api to get the user information
{
$user_info = file_get_contents('http://localhost/RestServer/api.php?action=get_user&id=' . $_GET ["id"]);
$user_info = json_decode($user_info, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<table>
<tr>
<td>Name: </td><td> <?php echo $user_info["last_name"] ?></td>
</tr>
<tr>
<td>First Name: </td><td> <?php echo $user_info["first_name"] ?></td>
</tr>
<tr>
<td>Age: </td><td> <?php echo $user_info["age"] ?></td>
</tr>
</table>
<a href="http://localhost/RestClient/index.php?action=get_userlist" >Return to the user list</a>
<?php
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost/RestServer/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<ul>
<?php foreach ($user_list as $user): ?>
<li>
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?><?php echo $user["name"] . "</a>"; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
}
?>
链接
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
是不正确的,必须是:
<a href='http://localhost/RestClient/index.php?action=get_user&id=3' alt='user_3'>Carbonnel</a>
注意标志的变化。
在您的示例中,$_GET['id']
必须始终是null
。
您的<a>
肯定有问题
标记属性使用单引号,查询字符串参数也使用单引号。
任何必须解释的程序都不知道href=
实际结束在哪里。
一种解决方案是对属性使用双引号("
),对值使用单引号(如果您需要的话)。
更改
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?>
至
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id=".$user ['id']." alt=user_".$user['id']."'>"; ?>