swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

Swoft\HttpClient\Adapter\CoroutineAdapter #301

Closed chongzi5210 closed 6 years ago

chongzi5210 commented 6 years ago
Q A
Bug report? yes/no
Feature request? yes/no
Swoft version x.y.z
Swoole version x.y.z (by php --ri swoole)
PHP version x.y.z (by php -v)
Runtime environment Win10/Mac/CentOS 7/Ubuntu/Docker etc.

Details

Describe what you are trying to achieve and what goes wrong.

使用 Swoft\HttpClient\Adapter\CoroutineAdapter post请求,报404, 同样地址参数curl的客户端可以正常返回, 看了下源码

1 2

这个execute 是不是应该改成

$client->execute((string)$request->getUri()->withFragment('')->getPath());  ?

Provide minimal script to reproduce the issue

// paste code
huangzhhui commented 6 years ago

@chongzi5210 提供一下你的操作代码 同时,由于前面的(string),这里实际执行的是 __toString() 方法

    /**
     * Return the string representation as a URI reference.
     * Depending on which components of the URI are present, the resulting
     * string is either a full URI or relative reference according to RFC 3986,
     * Section 4.1. The method concatenates the various components of the URI,
     * using the appropriate delimiters:
     * - If a scheme is present, it MUST be suffixed by ":".
     * - If an authority is present, it MUST be prefixed by "//".
     * - The path can be concatenated without delimiters. But there are two
     *   cases where the path has to be adjusted to make the URI reference
     *   valid as PHP does not allow to throw an exception in __toString():
     *     - If the path is rootless and an authority is present, the path MUST
     *       be prefixed by "/".
     *     - If the path is starting with more than one "/" and no authority is
     *       present, the starting slashes MUST be reduced to one.
     * - If a query is present, it MUST be prefixed by "?".
     * - If a fragment is present, it MUST be prefixed by "#".
     *
     * @see http://tools.ietf.org/html/rfc3986#section-4.1
     * @return string
     */
    public function __toString()
    {
        return self::composeComponents(
            $this->scheme,
            $this->getAuthority(),
            $this->path,
            $this->query,
            $this->fragment
        );
    }
huangzhhui commented 6 years ago

Swoft\HttpClient\Adapter\CurlAdapter 里面的写法也是一致的

curl_setopt($resource, CURLOPT_URL, (string)$request->getUri()->withFragment(''));
chongzi5210 commented 6 years ago
        $client = new Client([
            'adapter' => 'co',
            'base_uri' => 'http://xxx.xxx.xxx.xxx:xxxx',
        ]);
        $response = $client->post({path},[
            "json" => [
                "test"=>1
            ]
        ])->getResponse();

自己写了个php脚本测试可以获取到。但是项目调用一个后台服务,是python的tonado框架写的,报404。 查swoole的手册,发现post 方法有个说明 ,请求的path 不能带 http://domain. 我尝试修改了下,execute发送不带http://domain的path是可以的。 curl客户端确实是可以的,我猜是不是curl 和swoole_client实现不太一致?

c3184f62-c3c2-49bc-b2bc-3f65cda30705

另外 curl客户端那个忽略验证ssl证书的参数是不是写反了?