Google Goggles API workaround issues in PHP - php

I've been tearing my hair out with the following all day:
I'm attempting to send manual requests to Google Goggles with PHP-ported code from this blog post: http://notanothercodeblog.blogspot.com/2011/02/google-goggles-api.html
The original blog post has apparantly working code written in .NET, but my platform is PHP. The code I have is:
$googleID = substr(md5(rand()), 0, 16);
/* */
$url = 'http://www.google.com/goggles/container_proto?cssid=' . $googleID;
// $url = 'http://www.google.com/goggles/container_proto?cssid=';
// $googleCodes = array(0x22,0x00,0x62,0x3C,0x0A,0x13,0x22,0x02,0x65,0x6E,0xBA,0xD3,0xF0,0x3B,0x0A,0x08,0x01,0x10,0x01,0x28,0x01,0x30,0x00,0x38,0x01,0x12,0x1D,0x0A,0x09,0x69,0x50,0x68,0x6F,0x6E,0x65,0x20,0x4F,0x53,0x12,0x03,0x34,0x2E,0x31,0x1A,0x00,0x22,0x09,0x69,0x50,0x68,0x6F,0x6E,0x65,0x33,0x47,0x53,0x1A,0x02,0x08,0x02,0x22,0x02,0x08,0x01);
$googleCodes = array(34,0,98,60,10,19,34,2,101,110,186,211,240,59,10,8,1,16,1,40,1,48,0,56,1,18,29,10,9,105,80,104,111,110,101,32,79,83,18,3,52,46,49,26,0,34,9,105,80,104,111,110,101,51,71,83,26,2,8,2,34,2,8,1);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, array("CssidPostBody" => implode("",$googleCodes)));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/x-protobuffer",
"Pragma: no-cache"));
$response = curl_exec($handle);
/* */
print_r($response);
I keep getting the following response back from Google:
"Unparseable Container Request"
I believe my issue is with the format of $googleCodes. The original article has the following for it:
private static byte[] CssidPostBody = new byte[] { 34, 0, 98, 60, 10, 19, 34,
2, 101, 110, 186, 211, 240, 59, 10, 8, 1, 16, 1, 40, 1, 48, 0, 56, 1, 18,
29, 10, 9, 105, 80, 104, 111, 110, 101, 32, 79, 83, 18, 3, 52, 46, 49,
26, 0, 34, 9, 105, 80, 104, 111, 110, 101, 51, 71, 83, 26, 2, 8, 2, 34,
2, 8, 1 };
I realise this is an issue that is potentially very unique, but I'm hoping someone with experience with .NET byte arrays could point me in the right direction.

$googleCodes = array(34,0,98,60,10,19,34,2,101,110,186,211,240,59,10,8,1,16,1,40,1,48,0,56,1,18,29,10,9,105,80,104,111,110,101,32,79,83,18,3,52,46,49,26,0,34,9,105,80,104,111,110,101,51,71,83,26,2,8,2,34,2,8,1);
foreach ($googleCodes as $n=>$char) { $googleCodes[$n] = chr($char); }
I think these are codes for the characters, and I suspect they're being sent in binary. Either that, or they should be zero padded. Can you provide a link to the original method in the foreign language? We just want to emulate their method. I think your numbers are running together, so it's a question of the desired format.

Related

Wordpress incorrect query ordering by meta key numeric

I'm trying to order a custom taxonomy by a custom meta key. That meta key is the number of edition. Weekly, my client adds a new edition (the term of the "edition" taxonomy), and this number increases. At the moment, it is at 124.
So, I'm trying to order the admin list by this numeric meta key, this way:
add_filter("get_terms_args", "mytheme_get_terms_args", 10, 2);
static function mytheme_get_terms_args($args, $taxonomies)
{
if(is_admin() && in_array("edition", $taxonomies)){
$args['order'] = "ASC";
$args['orderby'] = "meta_value";
$args['meta_key'] = "_edition";
$args['meta_type'] = "DECIMAL";
}
return $args;
}
The expected result is all editions ordered as follows:
1, 2, 3, [...], 9, 10, 11, 12, 13, [...] 19, 20...
But, I'm getting this ordering instead:
1, 2, 3, [...], 9, 10, 100, 101, 102 , [...] 109, 11, 110, 111, 112, [...], 118, 119, 12, 120, 121, 122, 123, 13, 14, 15...
After the 10, instead following to 11, this crazy ordering goes to 100, 101, 102.
How can I order this correctly?
Thanks!

Output to file doesn't look the way it should

So I try to send some output to a file but it doesn't look the way I want.
I have a file with some random integer values , it looks like this:
78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73
4, 6, 9, 11, 16
9, 10, 16, 18, 20
1, 3, 8, 10, 15
Now what I want to do is for each of these lines print the average, max and min number into another file.
My code for this part is:
while (!feof($aFile))
{
$cnt += 1;
$anArray = array();
$anArray = explode(",", fgets($aFile));
foreach ($anArray as $value)
{
$value = (int)$value;
}
$line = "Line $cnt: Average=" . array_sum($anArray) / count($anArray) . " Max=" . max($anArray) . " Min=" . min($anArray);
fwrite($resultsFile, $line);
fwrite($resultsFile, "\n");
}
The problem is that the resultsFile looks like this:
Line 1: Average=70.6 Max= 85 Min= 60
Line 2: Average=9.2 Max= 16
Min=4
Line 3: Average=14.6 Max= 20
Min=9
Line 4: Average=7.4 Max= 15 Min=1
I couldn't exactly copy paste this , because when I do it pastes it the right way
The problem is that it changes line and then prints Min .
Total Average=50.533333333333 Total Max= 85 Total Min=1
This is kinda tricky and is a combination of several moments.
First - you have new line symbol (\n) in each string of your file.
So, for example string 9, 10, 16, 18, 20 is really a 9, 10, 16, 18, 20\n string.
When you explode this string by ,, last element of your $anArray is not 20, but 20\n (with new line).
Now you're trying to cast each element to int, but as by default php works with copy of array, your initial values in $anArray are unchaged. As correctly stated in comments - to apply changes to original array, use $&value notation:
foreach ($anArray as &$value)
{
$value = (int)$value;
}
And last - all following operations on your $anArray silenlty cast 20\n to int (20). But as noticed in max manual, for example:
The actual value returned will be of the original type with no conversion applied
So, in array ['9', '10', '16', '18', '20\n'] max value will be 20, but returned value will be 20\n which creates a new line and moves Min=... output to next line.

Generate random numbers from given character set. PHP

How to generate a random from the following list of numbers-
a) 1 to 50 (one from the list)
b) 6, 12, 18, 24, 30 (one from the list)
c) 7, 13, 19, 25, 31 (one from the list)
d) 3 to 75 (one from the list)
Try following that will help you
$fixarray1 = array(6, 12, 18, 24, 30);
$fixarray2 = array(7, 13, 19, 25, 31);
$arra1 = rand(1, 50);
$arra2 = array_rand($fixarray1,1);
$arra3 = array_rand($fixarray2,1);
$arra4 = rand(3, 75);
echo $arra1."-".$arra2."-".$arra3."-".$arra4;

How to diff file's apostrophe text in PHP

I want to diff 2 files and ignore checking lines that doesn't have apostrophe ("text") in PHP
For example:
File 1:
START
LTEXT "Tool Version:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT
EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT
File 2:
/*
* Translated by Saibamen
*
/
START
LTEXT "Wersja narzędzia:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT
EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT
I want to check if there's any difference between Tool Version: and Wersja narzędzia: strings.
Note: Files doesn't have this same schemas line by line - I want to start diff lines from line that have START in file.
You should first get content of each file and then use preg_match function with proper pattern to find version line and version string.
$fileContent1 = <<<TXT
dummy
dummy
LTEXT "Tool Version:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT
dummy
TXT;
$fileContent2 = <<<TXT
dummy
LTEXT "Wersja narzędzia:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT
dummy
dummy
TXT;
function diff_versions($leftContent, $rightContent) {
$diff = true;
$leftVersion = null;
$rightVersion = null;
$pattern = '/LTEXT "(Tool Version|Wersja narzędzia):", (.*)\R?/';
if (preg_match($pattern, $leftContent, $matches) !== 1) {
throw new Exception('Left content has no version line.');
}
$leftVersion = $matches[2];
if (preg_match($pattern, $rightContent, $matches) !== 1) {
throw new Exception('Right content has no version line.');
}
$rightVersion = $matches[2];
return array(
'diff' => $leftVersion === $rightVersion,
'leftVersion' => $leftVersion,
'rightVersion' => $rightVersion,
);
}
var_dump(diff_versions($fileContent1, $fileContent2));
Output:
array (size=3)
'diff' => boolean true
'leftVersion' => string 'IDC_STATIC, 70, 150, 80, 10, SS_RIGHT EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT' (length=89)
'rightVersion' => string 'IDC_STATIC, 70, 150, 80, 10, SS_RIGHT EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT' (length=89)
And then you should diff versions as you like.
Maybe this will give a point to start with:
$str1 = 'LTEXT "Tool Version:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT
EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT';
$str2 = 'LTEXT "Wersja narzędzia:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT
EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT';
$shortStr1 = substr($str1, 23);
$shortStr2 = substr($str2, 28);
echo "\n";
echo substr_compare($shortStr1, $shortStr2, 0, strlen($shortStr1));
echo "\n";
echo substr_compare($shortStr2, $shortStr1, 0, strlen($shortStr2));
Two compares if the Strings are not the same size
Will return 0 if same
Will return 1 if not same
For a better test you have to test via preg_match() and pre_match_all()
I think this - or at least the idea - will lead you to the answer.
Next time provide more Information and what you already have tried and many people here will even provide full solutions!
But for that you have to show what you already got and already tried. No one wants to do the work you are paid for, but many here are willing to help you, if you have any problem and tried hard enough!

How to create multi table side by side with fpdf

Can someone teach me how to create a table side by side like this with fpdf:
I'm trying to combine the multiple column with the creating table tutorial but failed. Any help would be appreciated. Thank you.
If you got struggled with fpdf, you can try to do it with Debenu Quick PDF.
This C# code returns the exact result which is on your picture. It is done with merged cells, but also you can use zero border width - it depends on your needs. Of course you can change the width and the color of the borders.
DPL.LoadFromFile("blank.pdf", "");
DPL.SetOrigin(1); //the top left page corner will be used for the origin
DPL.SetMeasurementUnits(0); //the units are approximately the same as a "point"
string content1, content2, content3;
content1 = "1<br>2<br>3<br>4<br>5<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>20";
content2 = "21<br>22<br>23<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>30";
content3 = "31<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>40";
int tableID1 = DPL.CreateTable(20, 3); //20 rows, 3 columns
DPL.SetTableColumnWidth(tableID1, 1, 1, 20); //width of the first column
DPL.SetTableColumnWidth(tableID1, 2, 3, 40); //width of the second and third column
DPL.SetTableRowHeight(tableID1, 1, 20, 10); //height of all rows
DPL.MergeTableCells(tableID1, 2, 1, 20, 1); //merge the cells from second row to 20. row and from 1. column to 1. column - merge the cells of the first column
DPL.MergeTableCells(tableID1, 2, 2, 20, 2); //merge the cells of the second column
DPL.MergeTableCells(tableID1, 2, 3, 20, 3); //merge the cells of the third column
DPL.SetTableCellContent(tableID1, 2, 1, content1); //add the content
DPL.DrawTableRows(tableID1, 50, 50, 700, 1, 0); //draw the table to the page
int tableID2 = DPL.CreateTable(20, 3); // second table
DPL.SetTableColumnWidth(tableID2, 1, 1, 20);
DPL.SetTableColumnWidth(tableID2, 2, 3, 40);
DPL.SetTableRowHeight(tableID2, 1, 20, 10);
DPL.MergeTableCells(tableID2, 2, 2, 20, 2);
DPL.MergeTableCells(tableID2, 2, 3, 20, 3);
DPL.MergeTableCells(tableID2, 2, 1, 20, 1);
DPL.SetTableCellContent(tableID2, 2, 1, content2);
DPL.DrawTableRows(tableID2, 160, 50, 700, 1, 0);
int tableID3 = DPL.CreateTable(20, 3); //third table
DPL.SetTableColumnWidth(tableID3, 1, 1, 20);
DPL.SetTableColumnWidth(tableID3, 2, 3, 40);
DPL.SetTableRowHeight(tableID3, 1, 20, 10);
DPL.MergeTableCells(tableID3, 2, 2, 20, 2);
DPL.MergeTableCells(tableID3, 2, 3, 20, 3);
DPL.MergeTableCells(tableID3, 2, 1, 20, 1);
DPL.SetTableCellContent(tableID3, 2, 1, content3);
DPL.DrawTableRows(tableID3, 270, 50, 700, 1, 0);
DPL.SaveToFile("tables.pdf");
You can find the description of the functions on this site:
http://www.debenu.com/docs/pdf_library_reference/CreateTable.php

Categories