使用 guzzle 测试并发时遇到的一个关于 PHP return 的问题

代码如下#

require './vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Promise;

$client = new Client(['base_uri' => '127.0.0.1/']);


// Initiate each request but do not block
$promises = [
    'test1' => $client->getAsync('test.php?id=1')
];
// Wait on all of the requests to complete.
$results = Promise\unwrap($promises);


// You can access each result using the key provided to the unwrap
// function.

die(var_dump($results['test1']->getBody()->getContents()));

test.php 文件如下#

$id = $_GET['id'];

class test{
    public function __construct($id)
    {
        $this->id = $id;
    }

    public function  handle()
    {
        //return $this->id;
        echo  $this->id;

    }
}

$result = new test($id);
$result ->handle();

疑问:为什么用 return 就返回不了数据,又用了 ajax 请求测试了一遍同样用 return 前台没接收到数据,但经常用的框架中为什么 ajax 请求用 return 就能返回对应的数据到前台?#

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

php 返回响应就是 echo 啊,,,laravel 用 return,但最终调用 $response->send (); 里面还是 echo,,,

5年前 评论
张无忌 (楼主) 5年前
讨论数量: 1

php 返回响应就是 echo 啊,,,laravel 用 return,但最终调用 $response->send (); 里面还是 echo,,,

5年前 评论
张无忌 (楼主) 5年前