小花 3.2。 - 将 UTF-8 字符发布到外部站点

发布于 2024-12-09 06:29:52 字数 1138 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的呼吸 2024-12-16 06:29:52

经过大量尝试后,我发现以下代码可以工作:

$data = http_build_query($params);
$request = Request::factory($url)
            ->method(Request::POST)
            ->body($data)
            ->headers('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');                       
$response = $request->execute();

这里的主要区别是:

  • 现在使用 ->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:

$data = http_build_query($params);
$request = Request::factory($url)
            ->method(Request::POST)
            ->body($data)
            ->headers('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');                       
$response = $request->execute();

The main differences here are that:

  • Now use ->body($data) instead of ->post($params)
  • Content-Type is set to application/x-www-form-urlencoded; charset=UTF-8
  • $data variable is created by using http_build_query() on the original $params array
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文