Im trying to import our customers from our Magento site to Shopify via the API. Im using PHP and CURL
My script works fine for most of our customers but its having trouble with any names that have foreign characters in, I get the question mark symbol when echoing the variables with the foreign characters in and the API call just fails and no customer is created.
Strangely if I copy the JSON object into Insomnia and run the API call it works fine even with the foreign characters so Im assuming Insomnia is adding some encoding that my script isnt.
Im assuming I have an issue with character set but not sure what I need to change it to or if I’ve set it wrong.
Heres my Curl code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
and Ive also tried declaring it as UTF-8
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
Is there anything obvious Ive got wrong?
Thanks
2
Answers
Sounds like you are forgetting to deal with UTF-8 properly. Whenever you see a question mark it shows you an encoding error. It means your source data was mangled. It means the source data was UTF-8, and then you read it as ASCII which cannot do UTF-8 properly, so then Shopify is going to see garbage as it works off of UTF-8 encoding.
If you are using a
mysqli
database connection to fetch the data from Magento, you may need to set the charset of the connection to utf8 so that PHP gets the data correctly from the database:$mysqli->set_charset("utf8")