简体   繁体   中英

Encoding in PHP curl post/put

I'm trying to send JSON via curl in PHP to a REST web service. It works fine when I am using English letters, but I'm getting errors for some international characters.

For some characters, such as Ð, ð and ó, I don't get an error, but when I look at the final product (I'm transmitting data to another database), the letters have been replaced by question marks. Só " Óli " becomes " ?li "

For other characters, such as Þ, I get a 500 error from the web service: "No mapping for the Unicode character exists in the target multi-byte code page"

When I try to use iconv, utf8_encode, utf8_decode or mb_convert_encoding and then use curl, I get the same error as with Þ.

In case, you are wondering, the other database does allow international characters (I am able to type them directly into the database and/or upload with CSV).

I've tried adding curl_setopt( $curl, CURLOPT_ENCODING, "" ); and curl_setopt( $curl, CURLOPT_ENCODING, "UTF-8" ); to the curl.

I've tried adding header('Content-Type: text/html; charset=utf-8'); to the php file (not sure if this has any impact on curl or if it only impacts how it displays in the browser).

$json = '
{ 
"CardUniqueId": "1",
"PersonName": "Þæö"
}';

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $uri . $action);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('api-key: '. $apikey));
curl_setopt( $curl, CURLOPT_ENCODING, "" ); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS,$json);
curl_setopt($curl,CURLOPT_POST, 1);
$resp = curl_exec($curl);
curl_close($curl);

Is there anything I am forgetting?

Note: I think I have done this before using the same curl settings, without problems, but it's been a while, so I might be mistaken. But I mention that because there is a chance the owner of the web service has made some changes on their end, causing this problem.

You're forgetting that JSON is a data transfer format, and not just a string. Use appropriate tools when working with it:

$data = ["CardUniqueId" => 1, "PersonName" => "Þæö"];
$json = json_encode($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM