PHP$\u GET在尝试第一个REST请求时为空
为了让我的头脑清醒过来,我正在学习/复制一个教程。“$\u get”是空的,我注意到正在调用的URL是空的,这里是一个副本PHP$\u GET在尝试第一个REST请求时为空,php,Php,为了让我的头脑清醒过来,我正在学习/复制一个教程。“$\u 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&
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>
观察“标志”的变化
在您的示例中,$\u GET['id']
必须始终为null
您肯定有问题
标记属性使用单引号,查询字符串参数也使用单引号
任何需要解释的程序都不知道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']."'>"; ?>