如何在PHP CURL POST请求中发送法语字符



我在$sms变量中有法语消息,我需要发送它,但我收到了错误&当我使用一些英语文本时,它会很好用。如何在Curl PHP中发送法语消息。

我使用了";内容类型:application/x-www-form-urlencoded;charset=utf-8";

下面是我的代码:

$sms='{"text":{"fr":"Bienvenue sur Gamebar! Accédez au service et gérer votre abonnement sur"}} '; 
$ordered_params = array(

'order' => '112423',
'prompt_content_args'=>$sms
);
$to_sign = '';
foreach ($ordered_params as $v)
{ 
$to_sign .= $v;
}
// maybe use utf8_encode($to_sign) if your php file is encoded iso-8859-1
$digest = hash_hmac('sha256', $to_sign, '8awIiFHqV8zlimG'); 

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://url.com", // no real
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "digest=$digest&order=112423&prompt_content_args=$sms",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded; charset=utf-8"
),
));
$result = curl_exec($curl);

该代码中的问题比注释中可以解决的问题更多:

$sms_data = [
"text" => [
// make sure that this is UTF-8 encoded _beforehand_. Check your editor's settings and/or source data.
"fr" => "Bienvenue sur Gamebar! Accédez au service et gérer votre abonnement sur"
]
];
$sms = json_encode($sms_data);
$ordered_params = [
'order' => '112423',
'prompt_content_args' => $sms
];
$to_sign = '';
foreach ($ordered_params as $v) { 
$to_sign .= $v;
}
$digest = hash_hmac('sha256', $to_sign, '8awIiFHqV8zlimG'); 
// consistent data structure
$curl_data = [
'digest' => $digest,
'order' => 112423,
'prompt_content_args' => $sms
];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://url.com", // no real
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true, // why on earth were you using CUSTOMREQUEST?
CURLOPT_POSTFIELDS => $curl_data, // let curl handle the encoding
CURLOPT_HTTPHEADER => [
"Content-Type: application/x-www-form-urlencoded; charset=utf-8"
],
]
);

如果出于某种原因,您需要在cURL调用之外对一组参数进行url编码,则需要使用http_build_query()

最新更新