arabic in json not display propeplay like "u0633\u0645\u0647" - json

I'm working on an project that fetch the arabic news from some server that use API to send news to android application and put it to my server.
I have created JSON POST page php and that working fine with me (fetch all i news i need it) only issue with the Arabic Characters and i have add charset=UTF-8 two time as shows in below code
As a result of this code, the output something like : "u0633\u0645\u0647 \u0627\u0644\u0644" since it should be something like "موقع الاخبار على مدار اليوم".
from that application the Arabic characters are displayed properly and I'm sure that they use UTF-8 as charset.
What could be the issue is?
btw i have do some changing header charset by using UTF-16 and UTF-32 instead of UTF-8 and they characters are changed as well to somrhing like Korian characters
$data_string = '{"para":{"pze":"2","date":"2014-06-16 13:55:17","did":"38","la":"ar","page":1,"token":"class","sub":"13"},"req":"a\/get_aet"}';
$ch = curl_init('http://NEWSWEBSITE.SOMETHING/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=UTF-8',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
header('Content-Type: application/json; charset=UTF-8');
print_r( $result);
`

Related

How to encode foreign characters for importing to shopify via API using PHP and CURL

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
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")
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.

CURL returning different responses

My code is this:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-type: application/json','Content-Length: ' . strlen($content), 'Cache-Control: no-cache'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if($result===FALSE)
{echo "cURL ERROR:".curl_error($ch);}
echo $result;
The problem is the fact that sometimes it works and sometimes it doesn't.
When it works I get a valid JSON response, like:
Working,
but when it doesn't, I get this:
Not Working
I should mention that I don't have access to the API server. Can anyone help me with any idea? Thank you!
The issue was that I was using a minification plugin that after a while was breaking the jQuery ajax call.
I solved it by implementing an inline script for the ajax call, using just javascript (no jQuery library dependency).

Post data using curl without showing the redirect URL in adress bar

Below is the my curl for senting some parameters to a url.But the parameter I m senting has sensitive datas like passwords and other things.But during posting the datas will be showed in the adress bar as redirect url.How to avoid this?.What to do to sent data without showing it in address bar?
$parameters='MerchantId='.$merchantId.'&Password='.$Password.'&ReferenceNo='.$ReferenceNo.'&RemoteIP='.$RemoteIP.'&Amount='.$Amount.'&BankId='.$BankId.'&Checksum='.$checksum.'&Name='.$Name.'&MobileNo='.$MobileNo.'&Email='.$Email;
//set POST variables
$url = 'https://payment.essecom.com/NetBanking/PayDirekt.jsp?'.$parameters;
//header('Location: '.$url);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/plain', 'Content-length: 900'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo curl_exec($ch);
curl_close($ch);
}
It's pretty easy to not include headers in output. Just add option CURLOPT_HEADER and set it to false. But without headers how can you be sure that request is completed successfully?

Sending files in curl request with header application-json

I am trying to send data in json format in curl request. so my body is like this
$body = json_encode($array);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($body),
'Content-Md5: ' . base64_encode(md5($body, true))
));
I can set content header this way --header "Content-Type: application/json" But when my content body will have files to be sent, will content type header application/json work?
Do i need to change the content-type header if my body has file in it?
No and yes, if you are talking about simulating a browser to "select" files and "upload" them to the server then the header will change to
Content-Type: multipart/form-data;
if on the other hand you are talking about using text files in place of long strings on the command line then no.
Read the notes in the answers at send/post xml file using curl command line

curl example of file download doesn't work

If I follow the box api 2.0 curl example to get the metadata of a file, I get the information. When I append /data to the url to download the file, curl says:
curl: (52) Empty reply from server
I did this with two files, both text files, same result.
The system here is OpenBSD 5.0.
In python 3, the same request raises an exception that says that the reply from the server contained a status line that is empty.
I had the same issue. The problem is that you need the CURLOPT_FOLLOWLOCATION option set to true. Box will execute a redirection from the API call to the actual file. Without it, it will return an empty response.
An empty response should be returned in the event that the file itself is empty. If, for instance, the two text files don't have any text in them, no text will be returned i.e. this appears to be expected behavior.
I'm getting the same issue. I have uploaded a file successfully, retrieved the file ID, then when I try to download the file, nothing happens. Like the original poster mentioned, if I take off the "/data" portion of the URL, then I can get all of the info back about the file successfully.
Here is my "download code" that gets called from a form with a simple "Download File" button for that particular file ID.
$boxkey = "ThisIsMyAPIKey";
$auth_token = $_POST['auth_token'];
$url = "https://www.box.com/api/2.0/files/".$_POST['file_id']."/data";
$header = array("Authorization: BoxAuth api_key=".$boxkey."&auth_token=".$auth_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_exec($ch);
curl_close($ch);
$url = "https://api.box.com/2.0/files/$fileId/content?access_token=$accessToken";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);