Php 如何在laravel中伪造guzzele post请求以获取响应数据
在laravel中,我需要伪造HTTP post请求Php 如何在laravel中伪造guzzele post请求以获取响应数据,php,laravel,guzzle,guzzlehttp,Php,Laravel,Guzzle,Guzzlehttp,在laravel中,我需要伪造HTTP post请求 try { return $client->request('POST', $url); } catch (GuzzleException $e) { return $e->getCode(); } 此请求返回401或带有一些数据的成功消息 现在我需要伪造这个来调用其他方法。我该怎么做 这是我的composer文件的一部分 "lara
try {
return $client->request('POST', $url);
} catch (GuzzleException $e) {
return $e->getCode();
}
此请求返回401或带有一些数据的成功消息
现在我需要伪造这个来调用其他方法。我该怎么做
这是我的composer文件的一部分
"laravel/framework": "5.1.*",
"guzzlehttp/guzzle": "~6.0.0",
引述:
Guzzle提供了一个模拟处理程序,可用于通过将返回值从队列中移出来实现带有响应或异常的HTTP请求
// Create a mock and queue two responses.
$mock = new MockHandler([
new Response(200, ['X-Foo' => 'Bar']),
new Response(202, ['Content-Length' => 0]),
new RequestException("Error Communicating with Server", new Request('GET', 'test'))
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
// The first request is intercepted with the first response.
echo $client->request('GET', '/')->getStatusCode();
//> 200
// The second request is intercepted with the second response.
echo $client->request('GET', '/')->getStatusCode();
//> 202
此外:
Guzzle附带一个node.js测试服务器,该服务器从队列接收请求并返回响应。测试服务器公开了一个简单的API,用于将响应排队并检查它接收到的请求
如果MoCKFANDER和测试WebServer不够,请考虑编写自己的汉德勒。有关详细信息,请参阅。
在哪里伪造?在测试方法中?@HCK:nope在控制器中。这样做的目的是从该URL获取响应。URL还没有定义。这就是为什么我需要假装itoh好的,检查@gordon的答案。这适用于GET请求。你能推荐一个职位吗。很好,顺便说一句。@Deemantha afaik http方法是不相关的。模拟处理程序将接受任何内容。