openswoole / ext-openswoole

Programmatic server for PHP with async IO, coroutines and fibers
https://openswoole.com
Apache License 2.0
808 stars 51 forks source link

HTTP Helper functions #291

Closed rootshell-do closed 1 year ago

rootshell-do commented 1 year ago
  1. What did you do? If possible, provide a simple script for reproducing the error.

Copy-paste HTTP helper function script from manual.

<?php

use function OpenSwoole\Coroutine\Http\get;

// Main coroutine context co::run(function() { // Coroutine 1 go(function() { $data = get('http://httpbin.org/get?hello=world'); $body = json_decode($data->getBody()); assert($body->headers->Host === 'httpbin.org'); assert($body->args->hello === 'world'); }); });

2. What did you expect to see?

Processed request response.

3. What did you see instead?

PHP Fatal error: Uncaught Error: Call to undefined function OpenSwoole\Coroutine\Http\get() in Standard input code:12

4. What version of OpenSwoole are you using (show your `php --ri openswoole`)?

Open Swoole => enabled Author => Open Swoole Group hello@openswoole.com Version => 22.0.0 Built => Dec 28 2022 22:03:58 coroutine => enabled with boost asm context epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled sockets => enabled openssl => OpenSSL 1.1.1s 1 Nov 2022 dtls => enabled http2 => enabled hook-curl => enabled pcre => enabled zlib => 1.2.13 mutex_timedlock => enabled pthread_barrier => enabled futex => enabled

Directive => Local Value => Master Value openswoole.enable_coroutine => On => On openswoole.enable_preemptive_scheduler => Off => Off openswoole.display_errors => On => On openswoole.unixsock_buffer_size => 8388608 => 8388608

5. What is your machine environment used (show your `uname -a` & `php -v` & `gcc -v`) ?

Linux valkyrie 5.15.72-gentoo-x86_64 #1 SMP Fri Oct 14 11:29:14 AST 2022 x86_64 AMD EPYC 7542 32-Core Processor AuthenticAMD GNU/Linux PHP 8.1.12 (cli) (built: Nov 10 2022 17:07:12) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.12, Copyright (c) Zend Technologies with Zend OPcache v8.1.12, Copyright (c), by Zend Technologies Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/11/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /var/tmp/portage/sys-devel/gcc-11.3.1_p20221209/work/gcc-11-20221209/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/11 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/11/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/11/include/g++-v11 --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/11/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --disable-libunwind-exceptions --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 11.3.1_p20221209 p3' --with-gcc-major-version-only --disable-esp --enable-libstdcxx-time --disable-libstdcxx-pch --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libssp --disable-libada --disable-cet --disable-systemtap --disable-valgrind-annotations --disable-vtable-verify --disable-libvtv --without-zstd --enable-lto --without-isl --enable-default-pie --enable-default-ssp Thread model: posix Supported LTO compression algorithms: zlib gcc version 11.3.1 20221209 (Gentoo 11.3.1_p20221209 p3)



You can also try the following OpenSwoole support channels:

* [Documentation](https://openswoole.com/docs) - Documentation for Open Swoole
* [Slack](https://goo.gl/forms/wooTTDmhbu30x4qC3) - Slack channel of Open Swoole
* [Discord](https://discord.gg/5QC57RNPpw) - Discord server of Open Swoole
doubaokun commented 1 year ago

Docs: https://openswoole.com/docs/modules/swoole-coroutine-http-client-get

Example:

co::run(function()
{
    $client = new OpenSwoole\Coroutine\Http\Client('httpbin.org', 80);

    $client->setHeaders([
        'Host' => 'httpbin.org',
        'User-Agent' => 'Chrome/49.0.2587.3',
        'Accept' => 'text/html,application/xhtml+xml,application/xml',
        'Accept-Encoding' => 'gzip',
    ]);

    // First GET request
    $client->get('/get?hello=world');

    var_dump($client->body);

    $client->close();
});