开发者

Curl post gives strange characters like åäö

I have a controller in grails with a post action. Now, when by php and curl trying to post to the grails contoller I get ? placeholders for characters like åäö, etc. If I create a smal开发者_高级运维l html form doring the same post the grails contoller receives the parameters as åäö and not as ?, etc.

What is the diffrence between below and how can I get curl to act as the html form example?

curl example:

$x = curl_init("http://localhost/post");      
curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));  
curl_setopt($x, CURLOPT_POST, 1);  
curl_setopt($x, CURLOPT_POSTFIELDS, "Foo=ö");  
curl_setopt( $x, CURLOPT_ENCODING, "UTF-8" );   
curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);  
$data = curl_exec($x);   
curl_close($x);

html form example:

<html> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head><title></title></head>
<body>
<form name="input" action="http://localhost/post" method="post" enctype="application/x-www-form-urlencoded">
<TEXTAREA NAME="Foo" COLS=10 ROWS=4 type=text>ö</TEXTAREA>
<input class="button" type="submit" value="send"/>
</form>
</body>
</html>


"UTF-8" is not a valid value for CURLOPT_ENCODING. You are only allowed identity, deflate or gzip. You'll need to set it in the Content-Type header:

curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded; charset=UTF-8'));  


  1. Use urlencode with curl_setopt

    curl_setopt($x, CURLOPT_POSTFIELDS, 'Foo=' . urlencode($value));
    
  2. Ensure that $value is indeed in utf-8, i.e. if it is a hand coded string literal ensure that php source code file is in utf-8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜