aliyun / openapi-sdk-php

Alibaba Cloud SDK for PHP
https://packagist.org/packages/alibabacloud/sdk
Other
521 stars 110 forks source link

Request parameters has malformed encoded characters. #167

Closed taroyutao closed 4 years ago

taroyutao commented 4 years ago

调用机器翻译API,使用PHP SDK存在中文的时候提示请求参数编码有问题,这个需要如何处理? 英文翻译到中文没有问题,但是中文翻译到英文报错提示:Request parameters has malformed encoded characters. 尝试UTF-8处理没有用。

header("Content-Type: text/html;charset=utf-8");

require DIR . '/vendor/autoload.php'; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException;

// Download:https://github.com/aliyun/openapi-sdk-php // Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md

AlibabaCloud::accessKeyClient('*', '*****') ->regionId('cn-hangzhou') ->asDefaultClient();

try { $result = AlibabaCloud::rpc() ->product('alimt') // ->scheme('https') // https | http ->version('2018-10-12') ->action('TranslateGeneral') ->method('POST') ->host('mt.cn-hangzhou.aliyuncs.com') // ->addHeader("charset", "UTF-8") // ->addHeader("content-type", "application/x-www-form-urlencoded") ->options([ 'query' => [ 'RegionId' => "cn-hangzhou", 'FormatType' => "text", 'SourceLanguage' => "zh", 'TargetLanguage' => "en", 'SourceText' => "北京欢迎你", // 'SourceText' => utf8_encode("北京欢迎你"), 'Scene' => "general", ], ]) ->request(); print_r($result->toArray()); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; } `

`