Jay Doshi
Jay Doshi

Reputation: 724

How to send French Characters in PHP CURL POST request?

I have French message in $sms variable and that I need to send it but I am getting error & while I using some English text, it will work fine. How to send French message in Curl PHP.

I have used "Content-Type: application/x-www-form-urlencoded; charset=utf-8"

below is my code:

$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);

Upvotes: 0

Views: 454

Answers (1)

Sammitch
Sammitch

Reputation: 32272

There are more problems in that code than could be addressed in comments:

$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"
    ],
]
);

If, for some reason, you need to url-encode a set of parameters outside of a cURL call, you would want to use http_build_query().

Upvotes: 2

Related Questions