French accents not working with Twilio SMS - sms

Twilio SMS messages work fine in English, but the French accents do not work. The accented characters are replaced by a ? sign. What can I do about this?

I found that converting the body to UTF-8 corrected this problem, and actually reduced the price by half. The difference is that I needed to utf8_encode the body, as shown below:
<?php
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
$phone = $_REQUEST['phone'];
$msg = utf8_encode ( $_REQUEST['msg']);
$sms = $client->account->messages->sendMessage("xxx-xxx-xxxx", $phone, $msg);

Related

How to limit the number of characters in laravel to convert to URL

I am using the google geocoding API to get latitude and longitude of addresses, the problem is that it is for Japanese users, which have "special" characters and when I use the service, the URL is too long
that results in a ZERO_RESULTS response
{
"results" : [],
"status" : "ZERO_RESULTS"
}
instead, if I delete some characters, I do get the results I want, what I'm looking for is to achieve that, for example
$customerAddress = "日本 〒462-0825 愛知県 名古屋市北区 大曽根3丁目 13-2V-Handsビル 1F"
do not exceed the number of characters allowed by the URL, which in the end I end up putting together like this
$geocodingUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=$customerAddress&key=$apiKey";
it doesn't matter if i remove characters from the end, but I need the address to not be SO specific so it can fit in the url
with this tool, we can verify what I say when placing the example address, we receive ZERO_RESULTS until we delete part of the text
how could I do it?
here is more documentation of the construction of the URL
URL encode to verify that it is too long by placing Japanese characters
You can use the Laravel Str helper to limit your string like this:
use Illuminate\Support\Str;
$rawCustomerAddress = "a very long address";
$maxAddressLength = 200;
$customerAddress = Str::limit($rawCustomerAddress, $maxAddressLength);
$geocodingUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=$customerAddress&key=$apiKey";
More details in the documentation

Laravel 8 multilanguage routes and how get the translated link of the same page

I'm trying to make my test app in multilanguage way.
This question has two correlated questions:
First question:
I followed the second answer in How to create multilingual translated routes in Laravel and this help me having a multilanguage site and the route cached, but I've a question and some misunderstanding.
It's a good practice overwrite an app config as they do int the AppServiceProver.php, making:
Config::set('app.locale_prefix', Request::segment(1));
Isn't better to work with the Session::locale in any case?
Second question:
In my case I've two languages, and in the navbar I want to print just ENG when locale is original language, and ITA when session locale is English.
If I'm in the Italian page, the ENG link in the navbar should point to the same English translated page.
Working with the method used in the other question, I hade many problems caused by the:
Config::set('app.locale_prefix', Request::segment(1));
We overwrite the variable in the config file local_prefix, and every time I switch to English language the locale_prefix will change to 'eng' and this sounds me strange, another thing I did is this:
if ( $lang && in_array($lang, config('app.alt_langs')) ){
return app('url')->route($lang . '_' . $name, $parameters, $absolute);
}
We use the alt_langs where are defined only the alternative languages, and this is a problem cause if I pass the local lang, in my case 'it', like lang parameter, this will not be found cause, from the description, the alt_lang should not contain the locale language and you will be able to get only the translated string.
If I change the:
if ( $lang && in_array($lang, config('app.alt_langs')) ){
return app('url')->route($lang . '_' . $name, $parameters, $absolute);
}
in:
if ( $lang && in_array($lang, config('app.all_langs')) ){
return app('url')->route($lang . '_' . $name, $parameters, $absolute);
}
Now using app.all_langs I'm able to choose which URL you want and in which language I want.
How do I get the translated URL?
In the blade file I need to get the translated URL of the page, and if read the other question, we used the $prefix for caching the routes and giving to the route a new name ->name($prefix.'_home'); in this way I can cache all the route and I can call the routes using blade without prefix {{ route('name') }} but, needing the translated url of the actual page a made this on the top of the view:
#php
$ThisRoute = Route::currentRouteName();
$result = substr($ThisRoute, 0, 2);
if ($result =='it' ){
$routeName = str_replace('it_', '', $ThisRoute);
$url = route($routeName,[],true,'en');
} else {
$routeName = str_replace('en_', '', $ThisRoute);
$url = route($routeName,[],true,'it');
}
#endphp
Doing this I get the actual route name that should be it_home I check if start with it_ or en_, I remove the it_ or en_ prefix and I get the translated URL, now you can use the $url as <a href="{{ $url" }}>text</a> cause if I call the {{ route('page') }} I get the link, with the locale language.
This code is not very good, I know, but I written in 5 minutes, need more implementation, and check, but for the moment is just to play with Laravel.
It's a good way?? How can I do it better (except the blade link retrieving)?? Many solution I found used middleware, but I would like to avoid a link in the navbar like mysite.com/changelang?lang=en
Is a good approach overriding the app.locale_prefix?
First
according to your question, it's a bad practice to save the preferences into .env or session because as soon as the session is finished the saved language will be removed also it's common when you need to store any preferences related to your website such as (Color, Font, Language, ...etc) you must store any of them into the cache.
Second
honestly, your code is a very strange and NOT common way and there are two ways to handle what do you need
First
There is a very helpful and awesome package called mcamara it'll help you too much (I recommend this solution).
Second
you can do it from scratch using the lang folder located in the resource folder and you must create files with the same count of the needed languages then use the keys that you'll define into these files into views and you can prefix your routes with the selected language you can use group method like so
Route::group(['prefix' => 'selected_lang'], function() {
Route::get('first_route', [Controller::class, 'your_method']);
});
or you can add the selected language as a query string like so localhost:8000/your_route?lang=en you can follow this tutorial for more info.

google-libphonenumber package does not validate chinese phone number in local format

The follow Chinese phone number is how you would dial domestically in China: 021 68336892
Internationally: +86 2168336892
The following code is to validate a Chinese phone number using google-libphonenumber
const phoneUtil = require("google-libphonenumber").PhoneNumberUtil.getInstance();
//+86 2168336892 //isValid
//021 68336892 //INVALID
const number = phoneUtil.parseAndKeepRawInput("021 68336892", "CH");
const isValid = phoneUtil.isValidNumber(number);
console.dir(isValid);
Only the international number passes validation. Is there a way with google-libphonenumber to have both pass validation?
Probably a little late to be helpful for you now, but "CH" is not the country code for China. Try using "CN".

Jsonp request with Swedish character gives null response

I'm using jsonp to request data from a web server to my application (built in sencha). The request has a dynamic parameter called 'sokt'. Sometimes the parameter has a swedish character (å, ä, ö) and sometimes it doesn't.
As long as there's no swedish charachter the server returns the expected result: for example:
http://mywebsite.se/jsonnew.php?sokt=test&_dc=1370095960312&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback31
But if there's a swedish character in the request, the server returns nothing. Example:
http://mywebsite.se/jsonnew.php?sokt=enastående&_dc=1370096101366&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback39
But then if i manually url encode the request enastående to enast%E5ende I get the expected result.
So, to summarize, the problem seems to be that the request from jsonp doesn't url encode the query string, it sends the unicode string which is not accepted by the php script that formats the response (which has a utf_8-header). If this really is the reason this is not working, which I'm not sure of, how would I solve this?
EDIT with code:
This is my request:
Ext.getStore('storen').setProxy({ type: 'jsonp', url: 'http://mywebsite.se/synonymer/jsonnew.php?sokt=' + param}).load()
And this is php script:
<?php
header('Content-Type: text/javascript; charset=utf8');
include("config.php");
$dbh = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$sokt = $_GET['sokt'];
$stmt = $dbh->prepare("SELECT * FROM table WHERE w1 = :sokt");
$stmt->bindParam(':sokt', $sokt);
$stmt->execute();
$output = array();
while ( $row = $stmt->fetch() ) {
$output[] = array("key" => utf8_encode($row['w2']));
}
$callback = $_REQUEST['callback'];
// Create the output object.
//start output
if ($callback) {
echo $callback . '(' . json_encode($output) . ');';
} else {
echo json_encode($output);
}
?>
You are attaching the value of sokt in the url of the proxy, hence the proxy want change anything. So you have to care about this yourself. There is a native method for that encodeURIComponent()
Ext.getStore('storen').setProxy({ type: 'jsonp', url: 'http://mywebsite.se/synonymer/jsonnew.php?sokt=' + encodeURIComponent(param)}).load()
As I can see someone other already answered this but deleted his answer cause you told him this want work, so here are some additional infomation:
Now your request should be send encoded like this
http://mywebsite.se/jsonnew.php?sokt=enast%C3%A5ende&_dc=1370096101366&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback39
Verify that by using your browsers developer tools. If that is so the frontend has done anything right and you will have to check the backend.
There you have to check if the param is URLdecoded and if not decode it
$sokt = urldecode($_GET['sokt']);
and you have to ensure that your database is really storing all the correct way. So use some tool like PHPMyAdmin (MySQL), ManagementStudio (MS SQL) or any query tool to see if your database behaves correct and the that the Data is stored in the correct format.
Checking all this should lead you to the error
When dealing with utf-8, first make sure everything is configured for utf-8
HTML
PHP, file store (file encoding)
Web Server
Database Columns (Collation)
Database Connection (Client and Server)
If done so, you're almost close to entirely forget about troubles with äöüß (No swedish chars here ;-) ).
And obviously you don't need any utf8_en|decode functions which do not work properly anyway, see the comments on php.net to these functions.
I've recently switched from ISO-8859-1 to utf-8 and it was a hell of work, but afterwards any "char conversion" was necessary any longer at all.
So: leaveing those äöü in an URL unencoded may still lead to problems. I don't kno Ext but assume that their doing a proper UTF-8 encoding of the extraParams as json requires that!
Have you tried
Ext.getStore('storen').setProxy({
type: 'jsonp'
,url: 'http://mywebsite.se/synonymer/jsonnew.php'
,extraParams: {
sokt: encodeURIComponent(params)
}
}).load();
Try using the extraParams option of the proxy instead of hardcoding it in the URL:
Ext.getStore('storen').setProxy({
type: 'jsonp'
,url: 'http://mywebsite.se/synonymer/jsonnew.php'
,extraParams: {
sokt: param
}
}).load();
Or, even simple, the params option of the load() method:
Ext.getStore('storen').load({params: {sokt: param});
Try decoding your param on the server side:
$sokt = urldecode($_GET['sokt']);
Try forcing your database connection to UTF8 before executing your query:
$dbh->prepare("SET NAMES 'utf8'")->execute();
Maybe one of these or the combination of both will work.

Creating barcode images

I need to send emails out to several thousand customers with a unique barcode present so they can redeem it either instore or online.
We have a list of coupon/barcode codes to use and have a way to dynamically pull these codes into the email so a customer will see a unique code. The problem is I need to somehow generate several thousand barcode images that are created using the unique codes. How can I solve this?
This would be perfect if our email marketing company had this functionality but unfortunately they don't:
http://www.emaildirect.com/blog/2011/11/create-unique-barcodes-with-emaildirect/
Any help would be greatly appreciated.
I have found my answer!
By using the barcode generator www.barcodesinc.com I generated a URL and input this into my email.
Eg: http://www.barcodesinc.com/generator/image.php?code=999999999&style=197&type=C128B&width=200&height=50&xres=1&font=3
I then changed the 999999999 in the URL to my conditional code to change to the specific code for that person and also bring back the barcode image for that code too!
I have found my answer!
By using the barcode generator www.barcodesinc.com I generated a URL and input this into my email.
Eg: http://www.barcodesinc.com/generator/image.php?code=999999999&style=197&type=C128B&width=200&height=50&xres=1&font=3
I then changed the 999999999 in the URL to my conditional code to change to the specific code for that person and also bring back the barcode image for that code too!
<img src="http://qrfree.kaywa.com/?s=8&d=your+text+here" alt="QRCode"/>
OR
http://qrfree.kaywa.com/?s=8&d=your+text+here
I'm no expert on this and haven't touched html but you could serialize each image and follow this example that has some sample code on QR code given a string.
Imports ThoughtWorks.QRCode.Codec
Dim objQRCode As QRCodeEncoder = New QRCodeEncoder()
Dim imgImage As Image
Dim objBitmap As Bitmap
objQRCode.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE
objQRCode.QRCodeScale = 2
objQRCode.QRCodeVersion = 5
objQRCode.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.L
imgImage = objQRCode.Encode("Test Data")
objBitmap = New Bitmap(imgImage)
objBitmap.Save("C:\QRCode.jpg")
Hi Try getting in touch with http://www.linktagger.com and ask if they can help. They provide Enterprise type services for the city where I live on maps and bus terminals so it might help you.
Here is an working example to generate barcode for the array of barcodes.We can
retrieve thousands of barcodes from csv file using pandas as well.
This example calls API and save the response in image(.png format) obtained as response from API call.
import shutil
import requests
data = [11111111111, 22222222222222222, 33333333333333, 4444444444444]
url = 'https://www.barcodesinc.com/generator_files/' + 'image.php?'
for d in data:
params = {
'code': d,
'style': '197',
'type': 'C128B',
'width': '200',
'height': '50',
'xres': '1',
'font': '3',
}
response = requests.get(url, params, stream=True)
with open('image-%s.png' % d, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response

Resources