<./>.dev./hood

가져가게 하기 -

// This is the API, 2 possibilities: show the app list or show a specific app by id.
// This would normally be pulled from a database but for demo purposes, I will be hardcoding the return values.

function get_app_by_id($id)
{
  $app_info = array();

  // normally this info would be pulled from a database.
  // build JSON array.
  switch ($id){
    case 1:
      $app_info = array("app_name" => "Web Demo", "app_price" => "Free", "app_version" => "2.0"); 
      break;
    case 2:
      $app_info = array("app_name" => "Audio Countdown", "app_price" => "Free", "app_version" => "1.1");
      break;
    case 3:
      $app_info = array("app_name" => "The Tab Key", "app_price" => "Free", "app_version" => "1.2");
      break;
    case 4:
      $app_info = array("app_name" => "Music Sleep Timer", "app_price" => "Free", "app_version" => "1.9");
      break;
  }

  return $app_info;
}

function get_app_list()
{
  //normally this info would be pulled from a database.
  //build JSON array
  $app_list = array(array("id" => 1, "name" => "Web Demo"), array("id" => 2, "name" => "Audio Countdown"), array("id" => 3, "name" => "The Tab Key"), array("id" => 4, "name" => "Music Sleep Timer")); 

  return $app_list;
}

$possible_url = array("get_app_list", "get_app");

$value = "An error has occurred";

if (isset($_GET["action"]) && in_array($_GET["action"], $possible_url))
{
  switch ($_GET["action"])
    {
      case "get_app_list":
        $value = get_app_list();
        break;
      case "get_app":
        if (isset($_GET["id"]))
          $value = get_app_by_id($_GET["id"]);
        else
          $value = "Missing argument";
        break;
    }
}

//return JSON array
exit(json_encode($value));




가져오기 -
<html>
 
<body>

<?php
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_app")
{
  $app_info
= file_get_contents('http://{Your Website}/api.php?action=get_app&id=' . $_GET["id"]);
  $app_info
= json_decode($app_info, true);
 
?>
   
<table>
     
<tr>
       
<td>App Name: </td><td> <?php echo $app_info["app_name"] ?></td>
     
</tr>
     
<tr>
       
<td>Price: </td><td> <?php echo $app_info["app_price"] ?></td>
     
</tr>
     
<tr>
       
<td>Version: </td><td> <?php echo $app_info["app_version"] ?></td>
     
</tr>
   
</table>
   
<br />
   
<a href="http://{Your Website}/REST_Client.php?action=get_app_list" alt="app list">Return to the app list</a>
 
<?php
}
else // else take the app list
{
  $app_list
= file_get_contents('http://{Your Website}/api.php?action=get_app_list');
  $app_list
= json_decode($app_list, true);
 
?>
   
<ul>
   
<?php foreach ($app_list as $app): ?>
     
<li>
        <a href=
<?php echo "http://{Your Website}/REST_Client.php?action=get_app&id=" . $app["id"]  ?> alt=<?php echo "app_" . $app_["id"] ?>><?php echo $app["name"] ?></a>
   
</li>
   
<?php endforeach; ?>
   
</ul>
 
<?php
} ?>
 
</body>
</html>



참고 URL -

http://blog.ijasoneverett.com/2013/02/rest-api-a-simple-php-tutorial/

'Server Side > PHP' 카테고리의 다른 글

curl 로 파라미터 전송  (0) 2017.03.30
배열 담은 함수 뽑기  (0) 2017.02.20
PHP json 인코딩 처리  (0) 2016.12.02
서버로 Request 요청 후 DB 인풋  (0) 2016.11.20
php interface 사용방법  (0) 2016.10.01