Failed to send SMS in Hindi - php

I need to send SMS in hindi, for this I need to pass the hindi string through URL.
As I am coading in php I used urlencode($hindimessage) on string and passed complete URL through file_get_contents(). On executing I got error:
Warning: file_get_contents(http://IP GOES HERE/smpp/sendsms?username=$name&password=******&to=$contact&from=DEMOTT&coding=3&&text=%E0%A4%AE%E0%A4%A8%E0%A5%80%E0%A4%B7+%E0%A4%95%E0%A5%81%E0%A4): failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported
Without using urlencode(), the server treats text as EMPTY STRING and rejects.
I also tried Using utf8_encode() encoding. I recive message in HTML tags like ही....
But when I use the API URL directly I am able to recive the message in hindi since API is Unicode API coding=3 enbled for Hindi text.(i.e API is working Properly)
Please Inform what kind of approach I need to adopt for sending message in both Hindi as well as in English.
Thanks in Advance

urlencode() is necessary if you are calling URL in file_get_contents() function.
You need to adopt CURL for sending message in both Hindi as well as in English.
$smsgatewayurl = 'http://IP GOES HERE/smpp/sendsms';
$post_data = array(); // All params including text message
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $smsgatewayurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
CURL is best option to call third party APIs compare to file_get_contents function. I have tested this above function with spring edge sms gateway including hindi text.

Set dcs coding =8 and convert your Hindi characters into Unicode characters and put it in the text field.This will work.

Related

Response from Google is different when using PHP curl vs. curl from command line

I am trying to port https://github.com/simon-weber/gpsoauth to PHP.
When I do a curl request from the command line, the response is as expected:
Command:
curl "https://android.clients.google.com/auth" --data "Email=<EMAIL>&Passwd=<PASSWD>&service=ac2dm&add_account=1"
Response:
SID=BAD_COOKIE
LSID=BAD_COOKIE
Auth=DQAAABEB....
services=mail,talk,ig,writely,reader...
Email=...
Token=oauth2rt_1/...
GooglePlusUpgrade=0
firstName=...
lastName=...
However, when I try the same call from PHP curl, the Token is missing.
Code:
$data = [
'Email' => $email,
'add_account' => 1,
'Passwd' => $passwd,
'service' => 'ac2dm',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.clients.google.com/auth');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
Response:
SID=BAD_COOKIE
LSID=BAD_COOKIE
Auth=DQAAABEB....
services=mail,talk,ig,writely,reader...
Email=...
GooglePlusUpgrade=0
firstName=...
lastName=...
I have tried using file_get_contents and even shell_exec, but the result is the same. Google accepts my credentials but doesn't send a Token
Why does PHP Curl behave differently? Do I have to add or remove some header?
Don't use http_build_query, it is for query string data, not application/x-www-form-urlencoded. The ! should not be encoded when part of a POST body. You can pass the array directly as the value for CURLOPT_POSTFIELDS. See the note about it on http://php.net/manual/en/function.curl-setopt.php.
RFC 1738 says:
Thus, only alphanumerics, the special characters "$-_.+!*'(),", and
reserved characters used for their reserved purposes may be used
unencoded within a URL.
Thanks #Paras.
The only difference between the two request was the content length - 80 for curl vs. 82 for PHP.
I have an exclamation mark in my password, which http_build_query() replaces with %21 and curl on the command line does not.
Google seems to accept both, but only sends the Token if it's a real ! and not %21.

Bad JSON being returned from Survey Monkey (get_survey_list)

I've been trying to pull Survey Data for a client from their Survey Monkey account, it seems that the more data their is the more likely illegal characters are introduced in to the resulting JSON string.
Below is a sample of what is returned on a bad response, every response is different and even shorter requests some times fail leaving me at a miss.
{
"survey_id": "REDACTED",
"title": "REDACTED",
"date_modified": "2014-XX-18 17:59:00",
"num_responses": 0,
"date_created": "�2014-01-21 10:29:00",
"question_count": 102
}
I can't fathom as to why this is happening, the more parameters in the fields option there are, the more illegal characters are introduced. It isn't just illegal invalid characters, some times random letters are thrown in as well which prevents me from handling the data correctly.
I am using Laravel 4 with the third party Survey Monkey library by oori
https://github.com/oori/php-surveymonkey
Any help would be appreciated in tracking down the issue, the deadline is pretty tight and if this can't be resolved I'll have to resort to asking the client to manually import CSV files which isn't ideal and introduces possible user error.
On a side note, I don't see this issue cropping up when using the same parameters on the Survey Monkey console.
O/S: Windows 8.1 with WAMP Server
Code used to execute the request
$Surveys = SurveyMonkey::getSurveyList(array
(
'page_size' => 1000,
'fields' => array
(
'title', 'question_count', 'num_responses', 'date_created', 'date_modified'
)
));
The SurveyMonkey facade is a custom package used to integrate the original Survey Monkey library located here:
https://github.com/oori/php-surveymonkey/blob/master/SurveyMonkey.class.php
Raw PHP cURL request
$header = array('Content-Type: application/json','Authorization: Bearer REDACTED');
$post = json_encode(array(
'fields' => array(
'title', 'question_count', 'num_responses', 'date_created', 'date_modified'
)
));
$post = json_encode($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=REDACTED");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
The above request returns the same troublesome characters, nothing else was used to get the response.
Using the following code
echo "\n".mb_detect_encoding($result, 'UTF-8', true);
This code shows the charset for the response, when successful and no illegal characters are present (there are still random characters in the wrong places) it returns that it is in fact UTF-8, when illegal characters are present false is returned so nothing is outputted. More often than not, false is returned.
Maybe I'm grossly oversimplifying the whole thing and apologies if so, but I have had these funny little chars pop in to results, too.
They were leading and trailing whitespace.
Can you trim data on retrieve and see if it still happens?

json_encode does not work on encrypted string

I'm having a little trouble, as i want to encrypt some post data i get from a form and then send them to my nodejs server in json format to put them into a database.
My Problem: i seem to be unable to post the data once it is encrypted. I can post the json string just fine, but not anything more:
My code:
$rsa->loadKey($keydata);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$encrypted = $rsa->encrypt("test");
$jsonArray = array(
'crypt' => $encrypted
);
$jsonArrayEncoded = json_encode($jsonArray);
echo $jsonArrayEncoded;
$ch = curl_init('https://..........');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonArrayEncoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
I don't even get the echo output. But the string seems to get encrypted, as i can echo that (a lot of charset errors + some random letters and numbers) and if i decode it in the php skript i get the correct result as well. I don't get any console warnings or errors, neither in chrome, nor firefox.
Anything i do wrong? (quite sure there is)
e: I'm using this as crypto library: http://phpseclib.sourceforge.net/rsa/examples.html#encrypt,enc1
edit2: well, as adviced in the comments i converted the string to utf8, but now it seems to be too long to be decrypted with my key... Tough o only encrypted the word "test"...
I think i have to dig deeper...
If anyone knows: for decryption I'm using the Ursa module for node.js with following code:
var buffer = new Buffer(req.body.crypt);
var data = private.decrypt(buffer, 'utf8', 'utf8', ursa.RSA_PKCS1_PADDING);
well, as adviced in the comments i converted the string to utf8, but
now it seems to be too long to be decrypted with my key... Tough o
only encrypted the word "test"...
It'd help to see your updated code that does that. In lieu of doing that...
json_encode doesn't natively handle binary data. My recommendation would be to do something like this:
$jsonArray = array(
'crypt' => bin2hex($encrypted)
);
$jsonArrayEncoded = json_encode($jsonArray);
echo $jsonArrayEncoded;
You'd need to compress it back down to binary, though, after you json decode'd it in Java.
Alternatively, you could do base64_encode and base64 decode it later.
The concern I'd have with utf8 encoding is that PHP's internal string type isn't utf8. If Java's is then that could cause problems it seems.
The json_encoding function has numerous flags that you can pass to it, which enable the function to parse particular character sets. The following call might solve the problems you are having
json_encode($jsonArray, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP );

Send UTF-16 encoded data with PHP curl

I'm building a php client to a web service that requires posted data to be encoded as UTF-16. How do i configure curl to encode my data in UTF-16 and also to decode the answer in UTF-16?
Some sample code:
$s = curl_init($url);
curl_setopt($s,CURLOPT_POST,1);
curl_setopt($s,CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($s,CURLOPT_POSTFIELDS,$data);
curl_setopt($s,CURLOPT_TIMEOUT, 5);
curl_setopt($s,CURLOPT_HTTPHEADER,array('Content-Type: text/plain'));
$result = curl_exec($s);
curl_close($s);
Adding an Accept-Encoding header does not seem to do the trick. Is it possible to encode my $data string in UTF-16 first and then pass a byte array to curl instead of a string?
Thank you for your answers!
First, you need to find out what's your data encoding. Then, it's your choice. Both iconv() and mb_convert_encoding() work pretty well.
Additionaly, you should inform about the encoding in the HTTP header:
curl_setopt($s,CURLOPT_HTTPHEADER,array('Content-Type: text/plain; charset=UTF-16'));

Google Maps API issue with non-English addresses (PHP/CURL)

I'm implementing Google Maps service with PHP/CURL and currently have a problem with Maps API.
I am sending queries to Google Maps with English and Russian addresses.
English address works fine, while Russian address fails with 602 (address not found)
Weird thing is that if I copy-paste curl query with Russian address to browser, it works fine (returns 200 and coords).
My code is
public static function google_geolocator($geoloc){
$geoloc = urlencode(implode(" ",$geoloc));
$query = "http://maps.google.com/maps/geo?q={$geoloc}&sensor=true&oe=utf8";
echo $query;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $query);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec($curl);
curl_close($curl);
$data = json_decode($json, TRUE);
return $data;
}
I searched through Internets and figured out some hacks with User-Agent and &oe=utf8 and so on, but none of those seem to work. The main thing that confuses me, again, is that this method works perfectly with address in English, but fails with Russian. Although pasting query into browser works perfect in both cases.
Thank you in advance!
small update: query like
http://maps.google.com/maps/geo?q=%D0%A2%D0%B0%D0%B8%D0%BB%D0%B0%D0%BD%D0%B4+%D0%9A%D0%BE+%D0%9F%D1%85%D1%83%D0%BA%D0%B5%D1%82&sensor=true&oe=utf8
works in Chrome, but doesn't work in Firefox (same 602), while
http://maps.google.com/maps/geo?q=Thailand+Koh+Phuket&sensor=true&oe=utf8
works fine in both
upd2
var_dumping($data) returns
{
"name": "Таиланд Ко Пхукет",
"Status": {
"code": 602,
"request": "geocode"
}
}
and name field is absolutely the same as in Chrome response.
upd3 alright, the thing is if i change location name in Russian slightly, both Chrome and FF return 200 and coordinates. The problem seems to be that Chrome is bit more "intellectual" while communicating with Google. Refactored method so location names are always provided in English no matter what current locale is. Looks like it's better not to mess with Google and character sets different from English.
The main thing that confuses me, again, is that this method works perfectly with address in English, but fails with Russian.
You are telling Google to expect UTF-8 data, but you're probably sending it something else. UTF-8 is a double-byte encoding - plain old ASCII characters consist of one byte, just as in ASCII; Russian and other characters of two or more. That's why the English alphabet often works, but as soon as other characters come into play, things break. Most likely, your Cyrillic characters are getting garbled (= stored as an encoding other than UTF-8) somewhere along the way, before they enter your query.
Make sure the values in $geoloc are UTF-8 encoded. If they come from a file, make sure that is UTF-8 encoded. If they come from a database, make sure the tables and the database connection are UTF-8 encoded.

Categories