我一直在寻找大约2个小时,我无法弄明白如何阅读最终的回应uri.
在先前版本的PHP Guzzle中,您只需致电即可$response->getEffectiveUrl()获得它.
我期望在新版本中有类似的东西,所以最终代码如下所示:
$response = $httpClient->post('http://service.com/login', [
'form_params' => [
'user' => $user,
'padss' => $pass,
]
]);
$url = $response->getEffectiveUrl();
Run Code Online (Sandbox Code Playgroud)
但是在最新版本$response中现在是一个GuzzleHttp\Psr7\Response并且没有方法可以让我检索uri.
我在这里阅读了重定向(http://guzzle.readthedocs.org/en/latest/quickstart.html#redirects),但它没有说明
谢谢@YauheniPrakopchyk
我如何能够获得Slim 3中Slim 3中的所有 get/ put/ post变量?
苗条2,
$allGetVars = $app->request->get();
$allPutVars = $app->request->put();
$allPostVars = $app->request->post();
Run Code Online (Sandbox Code Playgroud)
我怎么能在Slim 3中做到这一点?
例如, http://example.com/books/1?title=hello&content=world
我怎样才能在PARAMS title和content小井3呢?
苗条2,
$title = $app->request->get('title');
$content = $app->request->get('content');
Run Code Online (Sandbox Code Playgroud)
我怎么能在Slim 3中做到这一点?
我正在研究PSR-7接口并思考如何实现它们的方法.
我也一直在阅读这篇博文.显然,实现PSR-7接口的对象必须是不可变的.
因此,如果我从那时实现该withProtocolVersion方法,MessageInterface它将看起来像这样:
public function withProtocolVersion($version)
{
if ( $this->protocol === $version )
{
return $this;
}
$new = clone $this;
$new->protocol = $version;
return $new;
}
Run Code Online (Sandbox Code Playgroud)
我的问题确实是,为什么一成不变?为什么不简单地做一个return $this;?
并不是我担心它分配的内存量,我真的没有看到保持它不可变的任何好处.
就像博客文章所说,当你这样做时:
$request = $request
->withMethod('POST')
->withUrl(new Url('http://example.org/')
->withHeader('Content-Type', 'text/plain');
Run Code Online (Sandbox Code Playgroud)
然后创建了四个副本,但最终结果$request与我简单使用时相同return $this,对吧?
为什么决定保持它不变.那为什么我要做一个clone $this?它有什么好处?
我并没有真正想到这个想法.
我正在将我的项目从 PHP 7.0 更新到 PHP 8.0,但我无法确定是否允许显式分配resource为数据类型:
我现在所知道的是:
直到现在我读到:
我在某个地方错过了什么吗?
感谢您的时间。
为了更清楚起见,这就是我想要resource在项目中使用数据类型的方式(PSR-7 实现):
<?php
namespace MyPackages\Http\Message;
use Psr\Http\Message\StreamInterface;
/**
* Stream.
*/
class Stream implements StreamInterface {
/**
* A stream, e.g. …Run Code Online (Sandbox Code Playgroud) 我知道我可以使用fopen函数从文件名(真实的或URL)创建PHP流:
$stream = fopen('php://temp', 'r');
Run Code Online (Sandbox Code Playgroud)
然后,生成的stream($stream)是从URL创建的"stream"类型的资源.php://temp
但是我如何从资源中创建如上所述的流?
我为什么这么问?
我正在研究PSR-7库,我用类实现了PSR-7 StreamInterfaceStream.为了创建Stream实例,我决定也实现了StreamFactory.其接口StreamFactoryInterface在PSR-17(HTTP工厂)提案中定义.
在StreamFactoryInterface定义了一个名为方法createStreamFromResource,其中-符合其官方评论-应该:
从现有资源创建新流.
流必须是可读的并且可以是可写的.
因此工厂方法接收资源作为参数.并且,在其具体实现中,Stream创建了一个新对象 - 它也接收资源作为参数.
这是问题所在:
为简单起见,假设Stream该类仅适用于流,例如使用"stream"类型的资源.如果它收到的资源不属于"stream"类型,则会拒绝它.
那么,如果资源参数createStreamFromResource不是"流"类型的资源呢?如何将其转换为流,例如转换为"stream"类型的资源,以便我可以将其进一步传递给Stream使用它创建新对象的调用?是否有实现此任务的方法(PHP方法,函数或可能的转换函数)?
笔记:
testStream.php),我Stream用三种方式创建一个流,例如一个实例:一次直接,两次使用流工厂.StreamFactory带有方法的类createStreamFromResource.调用此方法应该是我创建流的第四种方法testStream.php.Stream和Response,这样就可以直接测试所有,如果你想.这两个类是我的真实代码的一个非常简化的版本. …它未被发送或未被正确接收.使用curl命令行直接(使用-d选项)或PHP(使用CURLOPT_POSTFIELDS)做的工作.
我从PSR-7请求开始:
$request = GuzzleHttp\Psr7\Request('POST', $url);
Run Code Online (Sandbox Code Playgroud)
我添加了身份验证标头,它正确地对API进行身份验证:
$request = $request->withHeader('Authorization', 'Bearer ' . $accessToken);
Run Code Online (Sandbox Code Playgroud)
然后我添加请求正文:
// The parameter for the API function
$body = \GuzzleHttp\Psr7\stream_for('args=dot');
$request = $request->withBody($body);
Run Code Online (Sandbox Code Playgroud)
我可以将消息发送到API:
$client = new \GuzzleHttp\Client();
$response = $client->send($request, ['timeout' => 2]);
Run Code Online (Sandbox Code Playgroud)
我收到的响应表明API根本看不到"args"参数.我已经尝试将身份验证令牌移动到args:
'args=dot&access_token=123456789'
Run Code Online (Sandbox Code Playgroud)
这应该可以工作,并且可以在命令行(-d access_token=123456789)中使用curl,但是在上面发送cia curl(6.x)时API也无法看到该参数.
我可以看到消息确实包含正文:
var_dump((string)$request->getBody());
// string(8) "args=dot"
// The "=" is NOT URL-encoded in any way.
Run Code Online (Sandbox Code Playgroud)
那么这里可能出现什么问题?参数是否未被发送,或者是否以错误的格式发送(可能是'='正在编码?),或者可能是使用的内容类型错误?使用Guzzle时很难看到"在网上"发送了什么,因为HTTP消息被格式化并发送了很多层.
编辑:调用本地测试脚本而不是远程API,我得到这个原始消息详细信息:
POST
CONNECTION: close
CONTENT-LENGTH: 62
HOST: acadweb.co.uk
USER-AGENT: GuzzleHttp/6.1.1 curl/7.19.7 …Run Code Online (Sandbox Code Playgroud) 我正在使用 Slim 4 框架实现一项服务,该框架几乎总是返回 JSON 响应。我试图使所有响应保持统一,其结构与此类似:
{
"status": "success",
"data": {"foo": "bar"} // the actual data relevant to the request
"messages": []
}
Run Code Online (Sandbox Code Playgroud)
在最基本的格式中,这是我需要执行的代码才能做出如下响应:
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
// Do something
$response
->getBody()
->write(json_encode([
'status' => 'success',
'data' => [
'foo' => 'bar'
],
'messages' => [],
]));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(200);
}
Run Code Online (Sandbox Code Playgroud)
现在,我一直在使用一个基本的帮助程序类,该类本质上将大部分样板包装成一些静态函数,因此我可以编写如下响应:
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
// Do something
$data = ['foo' => 'bar'];
return APIResponse::success($response, $data)->into();
}
Run Code Online (Sandbox Code Playgroud)
但是,现在我遇到一个问题,我想让响应稍微复杂一些,这将需要额外的依赖项,例如自定义序列化器类。天真的选择是继续将额外的依赖项传递给 …
我想知道是否有人可以向我解释服务器端请求的含义.它可能只是我不太了解的术语.对我而言,这听起来像是从服务器到客户端的请求,但我认为不是这样.
这是关于PHP PSR7.我试图找出为什么它同时具有RequestInterface和ServerRequestInterface.我无法在任何地方找到关于它的任何东西,我无法看到为什么这两个不仅仅合并到一个界面的原因.
当我尝试使用guzzleHttp时,我遇到了laravel 5.4的问题.这是我的代码.
use GuzzleHttp\Client;
$url = 'http://example.com';
$client = new Client();
$parameter = ['query' => ['name' => 'xxx', 'address' => 'yyy'], 'headers' => [ 'User-Agent' => 'xxxx', 'exceptions' => false, 'timeout' => 10 ]];
$res = $client->request('GET', $url, $parameter);
if ($res->getStatusCode() == 200)
{
$json = (string)$res->getBody();
return $json;
}
Run Code Online (Sandbox Code Playgroud)
我在日志中遇到此错误:错误异常:类GuzzleHttp\Psr7\Request的对象无法转换为字符串
我的代码出了什么问题?请帮助我.fyi,这个错误并不总是发生.有时它会显示此错误,有时会成功.
谢谢
场景一发送x-www-form-urlencoded数据
POST /path HTTP/1.1
Content-Type: application/x-www-form-urlencoded
foo=bar
Run Code Online (Sandbox Code Playgroud)
运行print_r($request->getParsedBody());返回正常:
Array
(
[foo] => bar
)
Run Code Online (Sandbox Code Playgroud)
运行print_r($request->getBody()->getContents());返回一个字符串foo=bar
场景2发送application/json数据
POST /path HTTP/1.1
Content-Type: application/json
{
"foo": "bar"
}
Run Code Online (Sandbox Code Playgroud)
运行print_r($request->getParsedBody());返回一个空数组。Array ( )
但是,运行print_r($request->getBody()->getContents());效果很好:
{"foo":"bar"}
这是预期的行为吗?
这意味着,如果我们要发送x-www-form-urlencoded数据,我们应该使用getParsedBody().
getBody()->getContents()如果我们发送 application/json?则应使用while 。
附加信息:
请求对象是使用以下命令创建的:
$request = \Laminas\Diactoros\ServerRequestFactory::fromGlobals(
$_SERVER, $_GET, $_POST, $_COOKIE, $_FILES
);
Run Code Online (Sandbox Code Playgroud)