v2.5.2
Giriş yap

PHP API mantigi

tia
516 defa görüntülendi

PHP API

Herkese selam. Yabanci bir arkadasim universitesi icin API calismasi yapmaya calisiyordu ancak bir suru hata aldik yardimci olabilecek arkadaslara simdiden cok tesekkurler.
Localhost tarafında f5 attığımızda geç yükleniyor hatta bayağı geç ve yüklendiğinde bile

App Name:
Warning: Illegal string offset 'app_name' in C:\xampp\htdocs\client.php on line 15
A
Price:
Warning: Illegal string offset 'app_price' in C:\xampp\htdocs\client.php on line 18
A
Version:
Warning: Illegal string offset 'app_version' in C:\xampp\htdocs\client.php on line 21
A
Return to the app list

bu tip hatalar veriyor

client.php
<?php
   <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <?php
      if(isset($_GET["action"]) && isset($_GET["id"]) && isset($_GET["action"]) == "get_app_by_id"){
        $app_info = file_get_contents('http://localhost/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>
        <p><a href="http://localhost/client.php?action=get_app_list" alt = "app list">Return to the app list</a></p>
        <?php
      }else{
        $app_list = file_get_contents('http://localhost/client.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://localhost/client.php?action=get_app_by_id&id=' .$app["id"] ?> alt=<?php echo "app_" . $app["id"] ?>>
                <?php echo $app["name"] ?>
              </a>
            </li>
			<?php endforeach; ?>
        </ul>
	  <?php } ?>
  </body>
</html>

?>
api.php
<?php
    <?php
/**
	This API will have 2 purposes
	A: Show the app list
	B: Show a specific app by id
*/

	function get_app_by_id($id){
		$app_info = 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(){
		$app_list = array(	array("id" => 1, "name" => "Web Demo"), array("id" => 1, "name" => "Audio Countdown"), array("id" => 1, "name" => "The Tab Key"), array("id" => 1, "name" => "Music Sleep Timer"));
		return $app_list;
	}
	
	$possible_url = array("get_app_list", "get_app_by_id");
	
	$value = "An error has occured!";
	
	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_by_id":
				if(isset($_GET["id"])){
					$value = get_app_by_id($_GET["id"]);
				}else{
					$value = "Missing argument";
				}
				break;
		}
	}

	exit(json_encode($value));
?>
?>
Cevap yaz
Cevaplar (1)
zafer
1149 gün önce

Doğrusunu söylemek gerekirse kodun her tarafında ve iş mantığında ciddi sıkıntılar var.

Bence php ve api özelinde örnek projeleri incelemek ve biraz daha bilgi sahibi olup tekrar denemek gerekir. Kolay gelsin.