为了账号安全,请及时绑定邮箱和手机立即绑定

从控制器返回数据 - Codeigniter

从控制器返回数据 - Codeigniter

PHP
开满天机 2023-04-28 14:36:04
我试图将数据返回到我的变量 $item_id 但变量返回空。我没有以正确的方式返回数据吗??item_id 应返回来自 serverMonitor.php 中的索引函数的“Alert”。请指教。谢谢项目.php function rest_state()  {     $id = 1;     $item_id =  $this->pass_to_res($id);     return $item_id //this should return "Alert"  }function pass_to_res($id)   {      $url = "https://example.com/serverMonitor?id=$id";      $ch = curl_init();      curl_setopt($ch, CURLOPT_URL, $url);      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");      $result = curl_exec($ch);      curl_close ($ch);      return $result;   }serverMonitor.php function index() {        $id = htmlentities($_GET['id']);        $word = "Alert";        return $word;    }
查看完整描述

1 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

函数的返回值并不总是对访问者可见。您应该使用 echo、print 或 print_r。


 function index() {

        $id = htmlentities($_GET['id']);

        $word = "Alert";

        echo $word;

        return true;

    }

你也可以使用 JSON with json_encode 来编码


function index() {

            $id = htmlentities($_GET['id']);

            $word = "Alert";

            echo json_encode(['word' => $word]);

            return true;

        }

和 json_decode 解码:


function pass_to_res($id)

   {

      $url = "https://example.com/serverMonitor?id=$id";

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, $url);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

      $result = curl_exec($ch);

      curl_close ($ch);


      $result = json_decode($result);

      return isset($result['word']) ? $result['word'] : false;

   }


查看完整回答
反对 回复 2023-04-28
  • 1 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信