I'm using quercus for appengine. I tried saving a long php string (> 1000 characters) but appengine won't allow me as String can only hold 500 characters. So I tried using appengine's Text datatype. It lets me save, however, when I retrieve the data from PHP, it returns me a resource() type instead of string.
Let me explain with code:
<?php
$a = new Text("this is a long string that contains more than 1000 characters");
$b = "this is a long string that contains more than 1000 characters";
$e = new Entity('Article');
$e->setProperty('content', $a); // this works fine
// $e->setProperty('content', $b); // will complain as strlen($b) is > 500
$db = DatastoreServiceFactory::getDatastoreService();
$id = KeyFactory::keyToString($db->put($e)); // works ok, returns the ID of Entity saved
?>
Now all's fine and dandy, but when I retrieve the content of $e, it will return me a resource() type data.
<?php
$q = new Query('Article');
$ps = $db->prepare($q);
foreach($ps->asIterable() as $i) {
echo gettype($i->getProperty('content')); // this will echo Object, which when var_dump'd, gives me a resource() which is not convertible to php string, thus I can't get the human readable value
}
?>
Is there any workaround to this? Any help is GREATLY appreciated as I've been pulling my hair for days...
Ok solved it by converting java object to string
$content = $i->getProperty('content');
if(is_object($content)) {
if($content instanceof Text) {
$content = $content->getValue();
}
}
Related
I have spent the whole day and night on this. Many answers from many people but none of which works for my case. Sorry for the noob question but I literally have spent whole yesterday until early morning for this.
So I got an old system to migrate to a new one.
Essentially from SQL server to Firebase. With a new structure, all new uid and the client wants the unicode data from old SQL server to be converted to the real emojis.
So it goes from SQL (in old server) => PHP (in old server to fetch the SQL data) => Flutter (in my laptop) => Firebase (Firestore).
In the SQL server there are strings like this for example:
Spring... fall...\ud83d\udc90...flowers!
Then the PHP code that retrieved from the SQL database return it like this:
// Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn,'utf8mb4');
header("Access-Control-Allow-Origin: *");
$result = $conn->query($sql);
if($result->num_rows > 0) {
$db_data = array();
while($row = $result->fetch_assoc()) {
$db_data[] = $row;
}
// Send back the complete records as a json
echo json_encode($db_data, JSON_UNESCAPED_UNICODE);
}else{
http_response_code(405);
echo "Query error";
}
$result->free();
$conn->close();
return;
In Dart (Flutter Web) I got the data like this:
final response = await http.get(urlRequest);
final rawRows = jsonDecode(response.body) as List<dynamic>;
rawRows.forEach((map) => print(map['description']));
This prints out: Spring... fall...\ud83d\udc90...flowers!
What I want is Spring... fall...💐...flowers!
I did a hardcoded print test (i.e: print('Spring... fall...\ud83d\udc90...flowers!'); and it gives exactly the result I want (i.e: It turns \ud83d\udc90 part of the string to 💐)
I have tried sending it as is to Firebase, it still doesn't recognize \ud83d\udc90 as emojis. It was stored in Firebase like this Spring... fall...\ud83d\udc90...flowers!. Which is totally understandable, I guess we need to convert it first.
I also have tried using converter in the PHP side like this: (thanks to Pedro Lobito)
function unicodeString($str, $encoding=null) {
if (is_null($encoding)) $encoding = ini_get('mbstring.internal_encoding');
return preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/u', create_function('$match', 'return mb_convert_encoding(pack("H*", $match[1]), '.var_export($encoding, true).', "UTF-16BE");'), $str);
}
Then the $db_data[] = $row; part in the PHP changed to $db_data[] = unicodeString($row);
But this solution only works for some emojis but not for all emojis, such as the case of 💐 above. It shows Spring... fall...?...flowers! instead.
So now I want to try to convert it in Dart instead since I'm more familiar with it. Unless someone could help to solve it in PHP as well.
Finally after working a couple of days I worked on my own solution.
So what I did was to use RegEx to detect the sequence in the String and replace them with emojis.
Declare Regular expressions for detecting the unicodes (in my case the unicode is something like (\uXXX):
// To detect a single unicode
static const regEx1 = '\\\\u([0-9a-fA-F]{4})';
// To detect a 2-bytes unicode
static const regEx2 = '$regEx1$regEx1';
Create a method to detect each RegEx. Then when a pattern is found, replace it with jsonDecode with added quotes in there. Like this jsonDecode('"${match.group(0)}"');.
The code:
static String _regExEmojiUnicode(String text, String regEx) {
final regexCheck = RegExp(regEx, caseSensitive: false);
String newText = '';
int _lastEndText = 0;
int _lastEndNewText = 0;
regexCheck.allMatches(text).forEach((match) {
final start = match.start;
final end = match.end;
final String replacement = jsonDecode('"${match.group(0)}"');
String startString;
newText == ''
? startString = '${text.substring(0, start)}$replacement'
: startString =
'${newText.substring(0, _lastEndNewText)}${text.substring(_lastEndText, start)}$replacement';
_lastEndNewText = startString.length;
_lastEndText = end;
newText =
'$startString${text.substring(end)}';
});
if(newText == '') newText = text;
return newText;
}
Create a method to check for the different cases of emojis:
static String replaceEmoji(String text) {
String newText = text;
// Checking for 2-bytes and single bytes emojis
if(newText.contains('\\u'))
newText = _regExEmojiUnicode(newText, regEx2);
if(newText.contains('\\u'))
newText = _regExEmojiUnicode(newText, regEx1);
return newText;
}
Then this works!!
I did this in haste, so if there is any improvements can be made to the RegEx patterns or anywhere else in the code, I'm open to any suggestions.
Thank you
I am 2 days into learning PHP and frustratingly I am struggling to GET and use a parameter passed into my API.
I have read the PHP $_GET documentation, it didn't take long, and also a number of SO pages about $_GET.
My use case is simple. I want to retrieve a list of records from MySQL db if they have a modified date greater than the passed in date. The function works if I hard code '2019-03-18 00:00:01' for example.
Using echo I can see I am getting the parameter.
In fact, copying the output from echo and using to update the function which I use in the argument returns the expected result.
I'm guessing this has been asked before but I can't find it. All the examples I read seem more challenging i.e. multiple variables or challenges with the function etc.
I must have made a newb mistake somewhere.
<?php
//Returns Lab Results Modified after the passed in date
include_once 'db_functions.php';
$db = new DB_Functions();
//Get JSON posted by Android Application
if (isset($_GET['date'])) {
$json = $_GET['date'];
echo $json;
// commenting out the next two line results in
// '2019-03-19 00:00:01' returning and not the json array.
$json = '2019-03-18 00:00:01';
echo $json;
$mod = $db->getLabResultsModifiedAfter($json);
$a = array();
$b = array();
if ($mod != false){
while ($row = mysqli_fetch_array($mod)) {
$b["ID"] = $row["ID"];
$b["last_modified"] = $row["last_modified"];
$b["LabRef"] = $row["LabRef"];
array_push($a,$b);
}
echo json_encode($a);
}
} else {
// Fallback behaviour goes here
}
?>
I haven't developed the app side function yet. I am using postman to pass in date of '2019-03-18 00:00:01' i.e. this for local.
Answer as provided by #aisby. I was using quotes in the passed in parameter.
The date you are passing in your URL does not following the format
'2019-03-18 00:00:01'. Try navigating to this URL ...
localhost:8080/its/… ... I have URL encoded the date 2019-03-18
00:00:01 in the querystring. I can see that you comment shows a date
query string starting with %27 which is the URL encoded single quote.
You should only send the value in query string variables. Not the
single or double quote string delimiters. – asiby 18 hours ago
I have successfully stored an image into a MongoDB database, since it is stored as a Base64 type information; I want to retrieve that BinData to a string. How do I do that? Here I am finding my document using input 'Email'.
<?php
$m = new MongoClient();
$db = $m->mydb2->mycol2;
$result = $db->find(array('Email'=>$em));
foreach( $result as $key){
$susername = $key['Email'];
$imagebody = $key['pic'];
}
echo $imagebody;
?>
EDIT:
As Haresh has said
$imagebody = $key['pic']->bin
works perfectly. But it returns me something like Raw data but if I write this
$imagebody = base64_encode($key['pic']->bin);
then it returns me exact Base64 format.
According to Documentation
To access the contents of a MongoBinData, use the bin field which return the string Mongo Binary Data
So Try this:
$imagebody = $key['pic']->bin
Hope this works for you.
I am new to AS3, and I had tried a few times to pass an array from php to AS3. But i can't manage to do it.
But i managed to narrow down the problem to 1 set of code, so wondering what do i need to change it.
When the function is this
function Asandler(event:Event){
var responseVariables:URLVariables = new URLVariables(event.target.data);
nobed = responseVariables.nobed ;
zip = responseVariables.zip;
Location = responseVariables.Location;
price = responseVariables.price;
}
It returns an error Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
But when i change it to
function Asandler(event:Event){
s1.test.text=event.target.data
}
It displays array with no problem, inside the dynamic text field.
php echo part
$solutions = array();
while ($row = mysqli_fetch_assoc($sql))
{
echo "nobed=".$solutions[1]=$row['nobed'];
echo "&zip=".$solutions[2]=$row['zip'];
echo "&Location=".$solutions[3]=$row['Location'];
echo "&price=".$solutions[4]=$row['price'];
}
Test Data string
nobed=100&zip=100&Location=100&price=100
New try, testing it with dynamic text field, it display the whole string.
var receivedValue:String = event.target.data.replace(/^\s+|\s+$/mg, "");
var test:Array = receivedValue.split(",");
s1.test.text =test[0];
But not too sure how to split the string up.
I'm building a script that takes the contents of several (~13) news feeds and parses the XML data and inserts the records into a database. Since I don't have any control over the structure of the feeds, I need to tailor an object operator for each one to drill down into the structure in order to get the information I need.
The script works just fine if the target node is one step below the root, but if my string contains a second step, it fails ( 'foo' works, but 'foo->bar' fails). I've tried escaping characters and eval(), but I feel like I'm missing something glaringly obvious. Any help would be greatly appreciated.
// Roadmaps for xml navigation
$roadmap[1] = "deal"; // works
$roadmap[2] = "channel->item"; // fails
$roadmap[3] = "deals->deal";
$roadmap[4] = "resource";
$roadmap[5] = "object";
$roadmap[6] = "product";
$roadmap[8] = "channel->deal";
$roadmap[13] = "channel->item";
$roadmap[20] = "product";
$xmlSource = $xmlURL[$fID];
$xml=simplexml_load_file($xmlSource) or die(mysql_error());
if (!(empty($xml))) {
foreach($xml->$roadmap[$fID] as $div) {
include('./_'.$incName.'/feedVars.php');
include('./_includes/masterCategory.php.inc');
$test = sqlVendors($vendorName);
} // end foreach
echo $vUpdated." records updated.<br>";
echo $vInserted." records Inserted.<br><br>";
} else {
echo $xmlSource." returned an empty set!";
} // END IF empty $xml result
While Fosco's solution will work, it is indeed very dirty.
How about using xpath instead of object properties?
$xml->xpath('deals/deal');
PHP isn't going to magically turn your string which includes -> into a second level search.
Quick and dirty hack...
eval("\$node = \"\$xml->" . $roadmap[$fID] . "\";");
foreach($node as $div) {