休息回声与 GET 中的返回 [英] rest echo vs return in GET

查看:39
本文介绍了休息回声与 GET 中的返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 php 和 rest 的新手,我正在尝试了解在使用 echoreturn 时我的 API 中发生了什么......

我试图找到最简单的可能场景来隔离我的 rest API 不返回任何值的问题,所以这里是:

我的服务器上有一个 test.php 文件,其中包含以下内容:

然后我使用唯一的标头Content-Type: application/json`"发出一个简单的请求

https://www.eswys.ch/tmp/test.php?t1=testhttps://www.eswys.ch/tmp/test.php?t2=test

结果分别是

测试...""

我真的很难理解这一点,为什么我的返回值不知何故丢失"了 - 对此有什么解释吗?!

解决方案

从函数返回实际上并没有向响应呈现任何内容.您可能已经看到人们在不同的框架中返回数据,但框架实际上是在幕后为您回显数据.

I am a newbie in php and rest and I'm trying to understand what is going on in my API when using echo vs return....

I've tried to get down to the simplest possible scenario to isolate an issue of my rest API not returning any value, so here is goes:

I have a test.php file on my server with following content:

<?php
    if(function_exists($_GET['t1'])) {
        echo $_GET['t1']();
    }
    else if(function_exists($_GET['t2'])) {
        return $_GET['t2']();
    }

    function test() {
        return json_encode("test...");
    }
?>

I then make a simple request using, as unique header 'Content-Type: application/json`

https://www.eswys.ch/tmp/test.php?t1=test
https://www.eswys.ch/tmp/test.php?t2=test

And results are, respectively

"test..." 
""

I really struggle to understand this, why is my returned value somehow "lost" - is there any explanation to this?!

解决方案

Returning from a function does not actually render any content to the response. You may have seen people returning data in different frameworks but the framework is actually echoing the data for you behind the scenes.

这篇关于休息回声与 GET 中的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆