国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

響應(yīng)

響應(yīng)實(shí)際上是一個(gè)support\Response對(duì)象,為了方便創(chuàng)建這個(gè)對(duì)象,webman提供了一些助手函數(shù)。

返回一個(gè)任意響應(yīng)

例子

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return response('hello webman');
    }
}

response函數(shù)實(shí)現(xiàn)如下:

function response($body = '', $status = 200, $headers = array())
{
    return new Response($status, $headers, $body);
}

你也可以先創(chuàng)建一個(gè)空的response對(duì)象,然后在適當(dāng)?shù)奈恢美?code>$response->cookie() $response->header() $response->withHeaders() $response->withBody()設(shè)置返回內(nèi)容。

public function hello(Request $request)
{
    // 創(chuàng)建一個(gè)對(duì)象
    $response = response();

    // .... 業(yè)務(wù)邏輯省略

    // 設(shè)置cookie
    $response->cookie('foo', 'value');

    // .... 業(yè)務(wù)邏輯省略

    // 設(shè)置http頭
    $response->header('Content-Type', 'application/json');
    $response->withHeaders([
                'X-Header-One' => 'Header Value 1',
                'X-Header-Tow' => 'Header Value 2',
            ]);

    // .... 業(yè)務(wù)邏輯省略

    // 設(shè)置要返回的數(shù)據(jù)
    $response->withBody('返回的數(shù)據(jù)');
    return $response;
}

返回json

例子

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return json(['code' => 0, 'msg' => 'ok']);
    }
}

json函數(shù)實(shí)現(xiàn)如下

function json($data, $options = JSON_UNESCAPED_UNICODE)
{
    return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
}

返回xml

例子

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        $xml = <<<XML
               <?xml version='1.0' standalone='yes'?>
               <values>
                   <truevalue>1</truevalue>
                   <falsevalue>0</falsevalue>
               </values>
               XML;
        return xml($xml);
    }
}

xml函數(shù)實(shí)現(xiàn)如下:

function xml($xml)
{
    if ($xml instanceof SimpleXMLElement) {
        $xml = $xml->asXML();
    }
    return new Response(200, ['Content-Type' => 'text/xml'], $xml);
}

返回視圖

新建文件 app/controller/FooController.php 如下

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return view('foo/hello', ['name' => 'webman']);
    }
}

新建文件 app/view/foo/hello.html 如下

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>webman</title>
</head>
<body>
hello <?=htmlspecialchars($name)?>
</body>
</html>

重定向

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return redirect('/user');
    }
}

redirect函數(shù)實(shí)現(xiàn)如下:

function redirect($location, $status = 302, $headers = [])
{
    $response = new Response($status, ['Location' => $location]);
    if (!empty($headers)) {
        $response->withHeaders($headers);
    }
    return $response;
}

header設(shè)置

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return response('hello webman', 200, [
            'Content-Type' => 'application/json',
            'X-Header-One' => 'Header Value' 
        ]);
    }
}

也可以利用headerwithHeaders方法來(lái)單個(gè)或者批量設(shè)置header。

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return response('hello webman')
        ->header('Content-Type', 'application/json')
        ->withHeaders([
            'X-Header-One' => 'Header Value 1',
            'X-Header-Tow' => 'Header Value 2',
        ]);
    }
}

你也可以提前設(shè)置header,最后設(shè)置將要返回的數(shù)據(jù)。

public function hello(Request $request)
{
    // 創(chuàng)建一個(gè)對(duì)象
    $response = response();

    // .... 業(yè)務(wù)邏輯省略

    // 設(shè)置http頭
    $response->header('Content-Type', 'application/json');
    $response->withHeaders([
                'X-Header-One' => 'Header Value 1',
                'X-Header-Tow' => 'Header Value 2',
            ]);

    // .... 業(yè)務(wù)邏輯省略

    // 設(shè)置要返回的數(shù)據(jù)
    $response->withBody('返回的數(shù)據(jù)');
    return $response;
}

設(shè)置cookie

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return response('hello webman')
        ->cookie('foo', 'value');
    }
}

你也可以提前設(shè)置cookie,最后設(shè)置要返回的數(shù)據(jù)。

public function hello(Request $request)
{
    // 創(chuàng)建一個(gè)對(duì)象
    $response = response();

    // .... 業(yè)務(wù)邏輯省略

    // 設(shè)置cookie
    $response->cookie('foo', 'value');

    // .... 業(yè)務(wù)邏輯省略

    // 設(shè)置要返回的數(shù)據(jù)
    $response->withBody('返回的數(shù)據(jù)');
    return $response;
}

cookie方法完整參數(shù)如下:

cookie($name, $value = '', $max_age = 0, $path = '', $domain = '', $secure = false, $http_only = false)

返回文件流

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return response()->file(public_path() . '/favicon.ico');
    }
}
  • webman支持發(fā)送超大文件
  • 對(duì)于大文件(超過(guò)2M),webman不會(huì)將整個(gè)文件一次性讀入內(nèi)存,而是在合適的時(shí)機(jī)分段讀取文件并發(fā)送
  • webman會(huì)根據(jù)客戶端接收速度來(lái)優(yōu)化文件讀取發(fā)送速度,保證最快速發(fā)送文件的同時(shí)將內(nèi)存占用減少到最低
  • 數(shù)據(jù)發(fā)送是非阻塞的,不會(huì)影響其它請(qǐng)求處理
  • file方法會(huì)自動(dòng)添加if-modified-since頭并在下一個(gè)請(qǐng)求時(shí)檢測(cè)if-modified-since頭,如果文件未修改則直接返回304以便節(jié)省帶寬
  • 發(fā)送的文件會(huì)自動(dòng)使用合適的Content-Type頭發(fā)送給瀏覽器
  • 如果文件不存在,會(huì)自動(dòng)轉(zhuǎn)為404響應(yīng)

下載文件

<?php
namespace app\controller;

use support\Request;

class FooController
{
    public function hello(Request $request)
    {
        return response()->download(public_path() . '/favicon.ico', '文件名.ico');
    }
}

download方法與file方法基本一致,的區(qū)別是
1、設(shè)置下載的文件名后文件會(huì)被下載下來(lái),而不是顯示在瀏覽器里
2、download方法不會(huì)檢查if-modified-since

獲取輸出

有些類庫(kù)是將文件內(nèi)容直接打印到標(biāo)準(zhǔn)輸出的,也就是數(shù)據(jù)會(huì)打印在命令行終端里,并不會(huì)發(fā)送給瀏覽器,這時(shí)候我們需要通過(guò)ob_start(); ob_get_clean(); 將數(shù)據(jù)捕獲到一個(gè)變量中,再將數(shù)據(jù)發(fā)送給瀏覽器,例如:

<?php

namespace app\controller;

use support\Request;

class ImageController
{
    public function get(Request $request)
    {
        // 創(chuàng)建圖像
        $im = imagecreatetruecolor(120, 20);
        $text_color = imagecolorallocate($im, 233, 14, 91);
        imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

        // 開(kāi)始獲取輸出
        ob_start();
        // 輸出圖像
        imagejpeg($im);
        // 獲得圖像內(nèi)容
        $image = ob_get_clean();

        // 發(fā)送圖像
        return response($image)->header('Content-Type', 'image/jpeg');
    }
}

分段響應(yīng)

有時(shí)候我們想分段發(fā)送響應(yīng),可以參考下面例子。

<?php

namespace app\controller;

use support\Request;
use support\Response;
use Workerman\Protocols\Http\Chunk;
use Workerman\Timer;

class IndexController
{
    public function index(Request $request): Response
    {
        //獲取連接
        $connection = $request->connection;
        // 定時(shí)發(fā)送http包體
        $timer = Timer::add(1, function () use ($connection, &$timer) {
            static $i = 0;
            if ($i++ < 10) {
                // 發(fā)送http包體
                $connection->send(new Chunk($i));
            } else {
                // 刪除不用的定時(shí)器,避免定時(shí)器越來(lái)越多內(nèi)存泄漏
                Timer::del($timer);
                // 輸出空的Chunk包體通知客戶端響應(yīng)結(jié)束
                $connection->send(new Chunk(''));
            }
        });
        // 先輸出一個(gè)帶Transfer-Encoding: chunked的http頭,http包體異步發(fā)送
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
        ]);
    }

}

如果你是調(diào)用的大模型,參考下面例子。

composer require webman/openai
<?php
namespace app\controller;
use support\Request;

use Webman\Openai\Chat;
use Workerman\Protocols\Http\Chunk;

class ChatController
{
    public function completions(Request $request)
    {
        $connection = $request->connection;
        // https://api.openai.com 國(guó)內(nèi)訪問(wèn)不到的話可以用地址 https://api.openai-proxy.com 替代
        $chat = new Chat(['apikey' => 'sk-xx', 'api' => 'https://api.openai.com']);
        $chat->completions(
            [
                'model' => 'gpt-3.5-turbo',
                'stream' => true,
                'messages' => [['role' => 'user', 'content' => 'hello']],
            ], [
            'stream' => function($data) use ($connection) {
                // 當(dāng)openai接口返回?cái)?shù)據(jù)時(shí)轉(zhuǎn)發(fā)給瀏覽器
                $connection->send(new Chunk(json_encode($data, JSON_UNESCAPED_UNICODE) . "\n"));
            },
            'complete' => function($result, $response) use ($connection) {
                // 響應(yīng)結(jié)束時(shí)檢查是否有錯(cuò)誤
                if (isset($result['error'])) {
                    $connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE) . "\n"));
                }
                // 返回空的chunk代表響應(yīng)結(jié)束
                $connection->send(new Chunk(''));
            },
        ]);
        // 先返回一個(gè)http頭,后面數(shù)據(jù)異步返回
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
        ]);
    }
}
編輯于2025-02-07 10:45:28 完善本頁(yè) +發(fā)起討論
贊助商