utf8 to ISO-8859-1 not converting some characters correctly through Curl

10,606

Solution 1

There is no euro sign in ISO-8859-1, so it gets substituted with a question mark. There's nothing you can do about it, except choosing something else to substitute it with.

The same goes for the other characters that get converted to ?s.

Solution 2

Some SMS protocols accepts "%80" for the Euro sign. So you could try substituting the "€" with "%80" and URL-encode the rest of the string using ISO-8859-1. It worked for me for some SMS protocols.

Share:
10,606
bones
Author by

bones

Developer of code. Work mainly with php, sql, html, js, css.

Updated on June 07, 2022

Comments

  • bones
    bones almost 2 years

    I have an application that is taking UTF8 encoded characters and needs to send them as part of xml through curl with ISO-8859-1 encoding.

    This is my test code:

    header('Content-Type: text/plain; charset=IS0-8859-1');
    
    $message = '§ ° " @ # € % & / ( ) = + ` ´ ^ ¨ * - _ : . ; ,';
    
    echo mb_convert_encoding($message, 'ISO-8859-1', 'UTF-8');
    
    //build xml to post
    $content =
        '<?xml version="1.0" encoding="ISO-8859-1"?>
        <mobilectrl_sms>
            <header>
                <customer_id>'.CUSTOMER_ID.'</customer_id>
                <password>'.PASSWORD_ID.'</password>
            </header>
            <payload>
                <sms account="'.SHORT_CODE.'">
                    <message><![CDATA['.mb_convert_encoding($message, 'ISO-8859-1', 'UTF-8').']]></message>
                    <to_msisdn>+12345678900</to_msisdn>
                </sms>
            </payload>
        </mobilectrl_sms>';
    
    $posturl = MT_URL;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $posturl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml", "Content-length: ".strlen($content), "charset=ISO-8859-1"));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    

    In the browser it almost works, I see § ° " @ # ? % & / ( ) = + ` ´ ^ ¨ * - _ : . ; ,

    notice the Euro Sign €

    But when it comes through as a text message I am seeing § ? " @ # ? % & / ( ) = + ? ? ^ ? * - _ : . ; ,

    I can't figure it out, I've tried utf8_decode also but that seems to make it worse. Am I missing something?

    Thanks