尝试第一个REST请求时,PHP $ _GET为空

时间:2013-11-29 15:09:30

标签: php

试图了解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
 }
?>

3 个答案:

答案 0 :(得分:2)

链接

<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

答案 1 :(得分:0)

<a>肯定有问题 您对tag属性使用单引号,然后对查询字符串参数使用。

任何必须解释的程序都不知道href=实际结束的位置。

一种解决方案是对属性使用双引号("),为值使用单引号(如果需要那些)。

答案 2 :(得分:0)

更改

<?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']."'>"; ?>