小花 3.2。 - 将 UTF-8 字符发布到外部站点
我正在尝试将 UTF-8 字符(即德国口音)发布到外部站点。然而,当它到达那里时,它看起来像这样:
ö
而不是这样:
ö
如果我使用适当的 UTF-8 标头 var_dump() ,我会看到应该是重音字母。
这是我在尝试 POST 时使用的代码:
$request = Request::factory($url)
->method(Request::POST)
->post($params)
->headers('Content-Disposition', 'form-data; name="postdata"')
->headers('Content-Type', 'text/plain; charset=UTF-8')
->headers('Content-Transfer-Encoding', '8bit');
$response = $request->execute();
基于此处的指南: http ://kohanaframework.org/3.2/guide/kohana/requests#external-requests
接收 URL 是基于 Java Spring 构建的。我已经使用 JMeter 测试了发布过程。当发布 UTF-8 重音字符时,捕获它们没有问题。上面的 PHP 示例中的标头使用与测试相同的设置。
这一定是 PHP 的问题,因为 JMeter 测试工作正常。
我还能够通过 JAVA 接收器站点将带重音的 UTF-8 字符从数据库拉到网站,然后通过 PHP/Kohana/HTML 并毫无问题地显示它们。
更多信息:
我发现,如果我运行
utf8_encode('ö');
// returns ö
,那么我想知道这是否发生在 POST 中。
I'm trying to post UTF-8 characters i.e. German accents to an external site. However when it gets there it appears like this:
ö
instead of this:
ö
If I var_dump() with the appropriate UTF-8 headers I see the accented letter as it should be.
Here's the code I'm using when attempting to POST:
$request = Request::factory($url)
->method(Request::POST)
->post($params)
->headers('Content-Disposition', 'form-data; name="postdata"')
->headers('Content-Type', 'text/plain; charset=UTF-8')
->headers('Content-Transfer-Encoding', '8bit');
$response = $request->execute();
Based on the guide here: http://kohanaframework.org/3.2/guide/kohana/requests#external-requests
The receiving URL is built on Java Spring. I have tested the posting process with the use of JMeter. When posting UTF-8 accented characters it had no problem catching them. The headers in the PHP sample above use the same settings as the tests.
It has to be an issue with the PHP as the JMeter tests worked fine.
I am also able to pull accented UTF-8 characters from a database through to the website via the JAVA receiver site, then through PHP/Kohana/HTML and display them without problems.
Further info:
I have found that if I run
utf8_encode('ö');
// returns ö
So I wonder if this is happening in the POST.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量尝试后,我发现以下代码可以工作:
这里的主要区别是:
->body($data)
而不是->post($ params)
Content-Type
设置为application/x-www-form-urlencoded; charset=UTF-8
$data
变量是通过在原始$params
数组上使用http_build_query()
创建的After a lot of playing around I have found the following code to work:
The main differences here are that:
->body($data)
instead of->post($params)
Content-Type
is set toapplication/x-www-form-urlencoded; charset=UTF-8
$data
variable is created by usinghttp_build_query()
on the original$params
array