php类中方法如何获取请求数据,php-如何在Laravel中获取HTTP请求正文内容?

我正在使用'saved!'创建API,并正在使用PHPUnit对其进行测试。我需要测试旧功能的兼容性,这是XML POST。 截至目前,我的第一个测试如下所示:

public function testPostMessages()

{

$response = $this->call('POST', 'xml');

$this->assertEquals(200, $response->getStatusCode());

}

这一切都很好。 我列表上的下一个实际上是通过POST发送XML数据。 因此,对于第二项测试,我有:

public function testPostMessagesContent()

{

$response = $this->call('POST', 'xml');

$this->assertTrue(is_valid_xml($response->getContent()));

}

该测试失败。 但是,我没有发送XML数据。 如何发送XML?

现在,在那之后,我需要添加功能以从请求中获取内容。 我知道有一个'saved!'对象可以与之交互,但是我不完全知道该调用哪种方法。 如何从控制器内部获取XML?

更新#1

在此期间,我得到了一些结果。 我当前的测试如下:

public function testPostMessagesContent()

{

$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));

$this->assertContains('~', $response->getContent());

}

我在那里只有波浪号,因为我知道它不匹配,这样我就可以看到整个响应。 在'saved!'中,我有:

class XmlController extends Controller {

public function index(Request $request)

{

return $request;

}

}

我从'saved!'的输出如下:

1) XmlTest::testPostMessagesContent

Failed asserting that 'POST xml HTTP/1.1

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Accept-Language: en-us,en;q=0.5

Content-Type: application/x-www-form-urlencoded

Host: localhost

User-Agent: Symfony/2.X

' contains "~".

我的参数和内容在哪里? 我觉得我只是在错误地使用'saved!'。

更新#2

我将控制器更新为使用'saved!',如下所示:

class XmlController extends Controller

{

public function index()

{

$content = Request::all();

return $content;

}

}

这返回了我的键值对,但没有返回内容。

1) XmlTest::testPostMessagesContent

Failed asserting that '{"key":"value"}' contains "~".

键值对很好; 这是进步。 但是,我真正需要的是内容,因为我将在请求的内容部分中接收该数据。 如果我使用'saved!',则会返回一个空白字符串。 这是我在测试中的电话:

$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));

这是我的测试结果:

1) XmlTest::testPostMessagesContent

Failed asserting that '' contains "~".

更新#3

我根本无法获得HTTP请求的内容主体。 由于XML无法正常工作,因此我继续使用JSON的API的REST部分。 这是我的测试之一:

public function testPostMessagesContent()

{

$response = $this->call('POST', 'messages', ['content' => 'content']);

$this->assertEquals('saved!', $response->getContent());

}

该测试通过。 如果使用curl,我也会接到一个成功的电话:

curl -X POST -d "content=my_new_content" "http://localhost:8000/messages"

这将返回'saved!',这很棒,但是如果我尝试在独立的PHP脚本中使用curl(模拟客户端),则返回以下内容:

Array ( [url] => http://localhost:8000/messages [content_type] => text/html; charset=UTF-8 [http_code] => 302 [header_size] => 603 [request_size] => 118 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.063977 [namelookup_time] => 0.000738 [connect_time] => 0.000866 [pretransfer_time] => 0.000943 [size_upload] => 12 [size_download] => 328 [speed_download] => 5126 [speed_upload] => 187 [download_content_length] => -1 [upload_content_length] => 12 [starttransfer_time] => 0.057606 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => ::1 [primary_port] => 8000 [local_ip] => ::1 [local_port] => 63248 [redirect_url] => http://localhost:8000 [request_header] => POST /messages HTTP/1.1 Host: localhost:8000 Accept: */* Content-type: text/xml Content-length: 12 ) Redirecting to http://localhost:8000.

这是我的curl命令,添加POST字段:

curl_setopt($ch, CURLOPT_POSTFIELDS, 'content=test');

在我看来,POSTFIELDS已添加到请求的正文中。 在这种情况下,我仍然有同样的问题。 我无法获取HTTP标头的正文内容。 注释掉我的验证后,我得到:

Array ( [url] => http://localhost:8000/messages [content_type] => text/html; charset=UTF-8 [http_code] => 200 [header_size] => 565 [request_size] => 118 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.070225 [namelookup_time] => 0.000867 [connect_time] => 0.00099 [pretransfer_time] => 0.001141 [size_upload] => 12 [size_download] => 6 [speed_download] => 85 [speed_upload] => 170 [download_content_length] => -1 [upload_content_length] => 12 [starttransfer_time] => 0.065204 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => ::1 [primary_port] => 8000 [local_ip] => ::1 [local_port] => 63257 [redirect_url] => [request_header] => POST /messages HTTP/1.1 Host: localhost:8000 Accept: */* Content-type: text/xml Content-length: 12 ) saved!

所以我收到了'saved!'消息。 大! 但是,现在我的数据库中有一个空白行。 不是很好。 它仍然看不到正文内容,仅看到标题。

回答标准

我正在寻找以下问题的答案:

为什么我无法获取请求的正文内容?

如何获取请求的正文内容?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值