Again, something that I have to keep in my blog.
I am developing a REST API, that decrypts an array of encrypted text. I post the array data in form of JSON data and nodejs keeps giving me an error "unexpected end of json input". But if I check the JSON data, everything looks fine.
stackoverflow saves my live again.
The explanation and the solution is shown in that link. Check the solution using "chunk"
Senin, 17 Juni 2019
Senin, 10 September 2018
Code Igniter, retrieving return value from view
You might need someday retrieving a return value from view. Well, I am glad that I finally knew it that it is possible.
So this is how we can do it.
The common usage of function to load a view is like this.
$this->load->view("myPage", $arrayOfData);
But if your view should return a value, then do this
$returnVal = $this->load->view("myPage", $arrayOfData, TRUE);
Yup, this function has a third parameter. If you set it as TRUE, than it will return the value, otherwise it will return CI Object.
So this is how we can do it.
The common usage of function to load a view is like this.
$this->load->view("myPage", $arrayOfData);
But if your view should return a value, then do this
$returnVal = $this->load->view("myPage", $arrayOfData, TRUE);
Yup, this function has a third parameter. If you set it as TRUE, than it will return the value, otherwise it will return CI Object.
Labels:
Codeigniter
Code Igniter ERROR syntax error, unexpected 'use' (T_USE)
Some of you might face this problem, when you try to integrate github library to your Project and you are using CodeIgniter.
The main problem is
You cannot put 'use' inside a function.
But you might have to use it inside your model. To solve it, I put the php file using the 'use' inside the 'views' directory and load it as a views.
In this example I am using the Github's shamir secret sharing library that is developed by Oliver Mueller and Stefan Gehrig.
model.php
<?php
public function shamirSecretSharing()
{
$data['message'] = 'this is a secret';
return $this->load->views('shamir/compute');
}
?>
And here is my compute.php located in "..\application\views\shamir\compute.php".
compute.php
<?php
require_once APPPATH. 'views/shamir/autoload.php';
use TQ\Shamir\Secret;
$shares = Secret::share($message, 3, 3);
return $shares;
?>
I'm not going to explain the function of the library. What I want to show is how I use the Library inside my model. If you ask me, what APPPATH mean. You can read it here.
The main problem is
You cannot put 'use' inside a function.
But you might have to use it inside your model. To solve it, I put the php file using the 'use' inside the 'views' directory and load it as a views.
In this example I am using the Github's shamir secret sharing library that is developed by Oliver Mueller and Stefan Gehrig.
model.php
<?php
public function shamirSecretSharing()
{
$data['message'] = 'this is a secret';
return $this->load->views('shamir/compute');
}
?>
And here is my compute.php located in "..\application\views\shamir\compute.php".
compute.php
<?php
require_once APPPATH. 'views/shamir/autoload.php';
use TQ\Shamir\Secret;
$shares = Secret::share($message, 3, 3);
return $shares;
?>
I'm not going to explain the function of the library. What I want to show is how I use the Library inside my model. If you ask me, what APPPATH mean. You can read it here.
Labels:
Codeigniter,
github
required, required_once, include Problem in Codeigniter
I had a problem integrating a library downloaded from github in codeigniter. One of the problem is the "required_once" function.
I'm sure there are many ways to solve this problem. I have two ways to share in this post.
Problem:
In every library downloaded from github, we need to call the autoload.php file at the start using "required_once". In some chases, required_once will be used almost in every php file. It is an old way to integrate github library in your code.
First Way:
Load it as a view.
For example, you need to include the autoload.php file.
Put the whole directory of the library inside the directory of 'views' and do this in the controller
controller.php
<?php
public function myFunc()
{
$this->load->view('yourLib/autoload');
$this->load->view('yourPageFile');
}
?>
Or you can do that inside the model as well'
aModel.php
<?php
public function modelFunc()
{
$var=$this->load->view('yourLib/autoload','',true);
your code ....
}
?>
Take a look at the load views. There are 3 parameters of the view function. The first parameter is the file of the page that you want to show, second parameter is for the variables that will be used in the page and the third parameter is set to TRUE. It will tell the CI that you want to return a value from view. If you don't set it to TRUE, it will return an CI-Object.
Second Way:
Use APPPATH macro. APPPATH is a macro referring your application Path.
If I do this
include_once APPATH."views/myLib/autoload.php";
that will give you
include_once "c:\xampp\htdocs\myApp\application\views\myLib\autoload.php";
I hope this can help you somehow
I'm sure there are many ways to solve this problem. I have two ways to share in this post.
Problem:
In every library downloaded from github, we need to call the autoload.php file at the start using "required_once". In some chases, required_once will be used almost in every php file. It is an old way to integrate github library in your code.
First Way:
Load it as a view.
For example, you need to include the autoload.php file.
Put the whole directory of the library inside the directory of 'views' and do this in the controller
controller.php
<?php
public function myFunc()
{
$this->load->view('yourLib/autoload');
$this->load->view('yourPageFile');
}
?>
Or you can do that inside the model as well'
aModel.php
<?php
public function modelFunc()
{
$var=$this->load->view('yourLib/autoload','',true);
your code ....
}
?>
Take a look at the load views. There are 3 parameters of the view function. The first parameter is the file of the page that you want to show, second parameter is for the variables that will be used in the page and the third parameter is set to TRUE. It will tell the CI that you want to return a value from view. If you don't set it to TRUE, it will return an CI-Object.
Second Way:
Use APPPATH macro. APPPATH is a macro referring your application Path.
If I do this
include_once APPATH."views/myLib/autoload.php";
that will give you
include_once "c:\xampp\htdocs\myApp\application\views\myLib\autoload.php";
I hope this can help you somehow
Labels:
Codeigniter,
github
Minggu, 02 September 2018
Sending Email with PHP
I got a request to create a web Application with a requirement sending a Warning via E-mail. First thing I did was googling, "sending Email with PHP", "how to send Email with PHP", ect. Thanks to many website and its author that give me a lot of information how to do that. I do really thank you guys for every information that you provide on your website.
and here is the sendMail.php
You can check your SMTP Configuration on your cpanel. That is already everything.
I found so many tutorial with the complete library and the example how to use it. Unfortunately, many don't work. BUT...I blame my self, I'm the one who doesn't know how to use it. Then, I stopped for a minute and try to figure it out, how is the logical steps for a web server using PHP to send an E-mail. let me straight to the point, we cannot send an E-mail just using the PHP. Maybe I'm wrong at this point, but if we really could do it, I'm sure that we should make some changes in our web server's configuration.
So, what can we do then? We create a Mail Client using PHP.
- Check the mail client manual setting in your web host. If you are using cpanel, check in Email Accounts --> Connect Devices --> Set up Mail Client
- Download PHPMailer here. Thanks to Marcus Bointon that provides us this library.
- Create the autoload.php, read it here how to do it.
Before I continue to the PHP code, lets create the autoload.php for the PHPMailer library. Here is how the autoload.php looks like:
I am using Code Igniter, I put the PHPMailer inside the view. So I load it as a view to use the library.
This is how my function in controller looks like:
<?php
/**
* An example of a project-specific implementation.
*
* After registering this autoload function with SPL, the following line
* would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
* from /path/to/project/src/Baz/Qux.php:
*
* new \Foo\Bar\Baz\Qux;
*
* @param string $class The fully-qualified class name.
* @return void
*/
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'PHPMailer\PHPMailer';
// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
Put that autoload.php inside PHPMailer directory.I am using Code Igniter, I put the PHPMailer inside the view. So I load it as a view to use the library.
This is how my function in controller looks like:
public function testMail()
{
$this->load->view('PHPMailer/autoload');
$this->load->view('sendMail');
}
and here is the sendMail.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.yourHost.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@yourHost.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 26; // TCP port to connect to
//Recipients
$mail->setFrom('user@yourHost.com', 'User's Name');
$mail->addAddress('recepient@test.com', 'Recipient Name'); // Add a recipient
//$mail->addAddress('recepient@test.com'); // Name is optional
$mail->addReplyTo('user@yourHost.com', 'User's Name');
//$mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject';
$mail->Body = 'write something here';
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
You can check your SMTP Configuration on your cpanel. That is already everything.
Labels:
Codeigniter,
github,
PHP,
PHPMailer,
Send Email
GitHub autoload.php
Some of you may need autoload.php after downloading a PHP library from gitHub. This post may be helpfull.
Here is how autoload.php looks like:
I'd like you to take a look at the '$prefix = "Project\\";'. What you have to do is just change the "Project" with the prefix used referring to $base_dir directory. The easiest way to find out is by opening the example.
For example, I was using Mike42 library to print to POS printer. Here is just a part of the example code:
In Mike42's case, the autoload.php will be:
The prefix Mike42 refers to directory /src/Mike42.
I hope this can help you
Here is how autoload.php looks like:
<?php
/**
* Users who do not have 'composer' to manage dependencies, include this
* file to provide auto-loading of the classes in this library.
*/
spl_autoload_register ( function ($class) {
/*
* PSR-4 autoloader, based on PHP Framework Interop Group snippet (Under MIT License.)
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
*/
$prefix = "Project\";
$base_dir = __DIR__ . "/src/";
/* Only continue for classes in this namespace */
$len = strlen ( $prefix );
if (strncmp ( $prefix, $class, $len ) !== 0) {
return;
}
/* Require the file if it exists */
$relative_class = substr ( $class, $len );
$file = $base_dir . str_replace ( '\\', '/', $relative_class ) . '.php';
if (file_exists ( $file )) {
require $file;
}
} );
?>
I'd like you to take a look at the '$prefix = "Project\\";'. What you have to do is just change the "Project" with the prefix used referring to $base_dir directory. The easiest way to find out is by opening the example.
For example, I was using Mike42 library to print to POS printer. Here is just a part of the example code:
<?php
/* Example print-outs using the older bit image print command */
require __DIR__ . '/../autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
In Mike42's case, the autoload.php will be:
<?php
/**
* Users who do not have 'composer' to manage dependencies, include this
* file to provide auto-loading of the classes in this library.
*/
spl_autoload_register ( function ($class) {
/*
* PSR-4 autoloader, based on PHP Framework Interop Group snippet (Under MIT License.)
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
*/
$prefix = "Mike42\";
$base_dir = __DIR__ . "/src/Mike42/";
/* Only continue for classes in this namespace */
$len = strlen ( $prefix );
if (strncmp ( $prefix, $class, $len ) !== 0) {
return;
}
/* Require the file if it exists */
$relative_class = substr ( $class, $len );
$file = $base_dir . str_replace ( '\\', '/', $relative_class ) . '.php';
if (file_exists ( $file )) {
require $file;
}
} );
?>
The prefix Mike42 refers to directory /src/Mike42.
I hope this can help you
Labels:
autoload.php,
github
Jumat, 17 Agustus 2018
Direct Print to POS Printer from Javascript on Client Side
As you have already known, web browser is like a sandbox. It is not allowed to access anything else on PC. That means, we need a program or background service as an interface to send the data from the web to printer. mike42 provides perfect PHP library that allows direct print from PHP. Unfortunately, PHP is on server side. We still need this library, since mike42 provides us functions that return ESC/POS data in binary. This data is what we need.
Next, since we want to print from web on client side, we need a way to direct print from javascript and, as I mentioned above, a third party service that allows us sending data to the printer. For that purpose, Kwickpos has that program written in python.
I use POS Printer Taffware ZJ-5890K. It is a low-priced thermal POS Printer and it is a good printer.
These are what we need for the PC on client side:
escpos-php-development is the name of the directory where library located.
Now here is how the "print.php" looks
print.php:
Take a look at the ajax function when it call xhttp.send("p=POSPRINTER&data="+rawdat). the "p" is for the name of the shared Printer, you may change the value of "p". The "data" is the bas64 encoded binary data of raw ESC/POS commands.
If you are using chrome, you need to install CORS extension. You need it, because the ajax tries to open a url outside the web application domain. Your web domain may be myweb.com and the ajax will try to access localhost, which is outside myweb.com domain. It may lead to security issues, chrome will warn you by the time you are activating this extension.
Actually that is already everything. You may have some question how to start the KwickPython on window startup or how to start it as windows service, just refer to the KwickPython website and just google to find how to start the script as windows service.
I hope this posting can help you somehow.
[Updated]
RUN KwickPython as a Service
To be able to print on client side, you should activate the KwickPython. On the official Website of KwickPython they already told us that there are two ways to start it automatically. Call it as a job and it will activate the cmd console or call it as service with no cmd console. The main problem if you call it as a job, the service will stop if the console is closed. So, its better if the service keep running although the console is closed. Then we should run it as a service. Unfortunately, it is not that easy to execute a command as a service.
So this is how I do it. I run the KwickPython at the startup as batch job with no console.
That's it. Try to reboot your windows. After your windows finish booting, open cmd and check it using 'netstat -tan' whether port 9100 is already in Listening state. Try to print something. It works fine in my project.
I hope this can help you....
Next, since we want to print from web on client side, we need a way to direct print from javascript and, as I mentioned above, a third party service that allows us sending data to the printer. For that purpose, Kwickpos has that program written in python.
I use POS Printer Taffware ZJ-5890K. It is a low-priced thermal POS Printer and it is a good printer.
These are what we need for the PC on client side:
- KwickPython, you can download it from its web or download the script here
- install Python. Go googling, download and install the latest version
- Install Pywin32. Find the version that is compatible with your Python's version
- Install the driver of the POS Printer and share it as "POSPRINTER". You can refer to this tutorial if you have never shared printer before.
- After installing all of them, you need to add python's file path to Environment Variables. You can read it here. I'm using windows 8 and tested it in windows 10 too, the file path to python is C:\Users\[username]\AppData\Local\Programs\Python\Python37-32. Replace "[username]" with the real path on your PC.
Do these steps:
- Open cmd console by Winkey-R, type "cmd" and Enter
- Make KwickPython cgi-bin directory:C:\KwickPython\htbin
- Save KwickPython script as:C:\KwickPython\htbin\kp.py
- Start the KwickPython:C:\KwickPython> python -m http.server --cgi 9100
Actually, you can read the more detailed explanation in KwickPython website. The script can do some other things, like return the list of active Printer, so you can choose which printer you want to use. My focus in this post is just how to use it for printing.
I assume, you have already started the KwickPython right now. The script listen to port 9100, you may change the port number if you like.
On Server Side you need mike42 PHP Library, download it here.
I forget to mention earlier, that I'm using CodeIgniter. I put the mike42 PHP Library inside the directory "view". And here are the controller and the view for printing.
Controller:
I assume, you have already started the KwickPython right now. The script listen to port 9100, you may change the port number if you like.
On Server Side you need mike42 PHP Library, download it here.
I forget to mention earlier, that I'm using CodeIgniter. I put the mike42 PHP Library inside the directory "view". And here are the controller and the view for printing.
Controller:
public function printPOS()
{
$this->load->view('escpos-php-development/autoload.php');
$this->load->view('print');
}
escpos-php-development is the name of the directory where library located.
Now here is how the "print.php" looks
print.php:
<?php
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
use Mike42\Escpos\CapabilityProfile;
/* Open the printer; this will change depending on how it is connected */
try
{
$connector = new DummyPrintConnector();
$profile = CapabilityProfile::load("TSP600");
$printer = new Printer($connector);
$printer -> text("hallo, direct Print From Javascript\n");
$printer-> feed(3);
$data = $connector -> getData();
$base64data=base64_encode($data);
$printer -> close();
}
catch(Exception $e)
{
echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
}
?>
<input type="hidden" id="rawdata" value="<?php echo $base64data;?>">
<script>
var dat="";
window.onload = callFunction();
function callFunction()
{
setInterval(ajax(),500);
}
function ajax()
{
var rawdat=document.getElementById('rawdata').value;
var xhttp = new XMLHttpRequest();
url = 'http://localhost:9100/htbin/kp.py';
xhttp.open("POST", url, false); //browser has to wait until the data finished loaded
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status == 200)
{
alert(this.responseText);
}
}
xhttp.send("p=POSPRINTER&data="+rawdat);
}
</script>
Take a look at the ajax function when it call xhttp.send("p=POSPRINTER&data="+rawdat). the "p" is for the name of the shared Printer, you may change the value of "p". The "data" is the bas64 encoded binary data of raw ESC/POS commands.
If you are using chrome, you need to install CORS extension. You need it, because the ajax tries to open a url outside the web application domain. Your web domain may be myweb.com and the ajax will try to access localhost, which is outside myweb.com domain. It may lead to security issues, chrome will warn you by the time you are activating this extension.
Actually that is already everything. You may have some question how to start the KwickPython on window startup or how to start it as windows service, just refer to the KwickPython website and just google to find how to start the script as windows service.
I hope this posting can help you somehow.
[Updated]
RUN KwickPython as a Service
To be able to print on client side, you should activate the KwickPython. On the official Website of KwickPython they already told us that there are two ways to start it automatically. Call it as a job and it will activate the cmd console or call it as service with no cmd console. The main problem if you call it as a job, the service will stop if the console is closed. So, its better if the service keep running although the console is closed. Then we should run it as a service. Unfortunately, it is not that easy to execute a command as a service.
So this is how I do it. I run the KwickPython at the startup as batch job with no console.
- There are 3 Files that I need: kp.py that I put inside 'htbin' directory, a bat file to execute the command activating the python service and a vbs file to call the bat file without console.
- Download the kp.py and put it inside 'htbin' directory
- put the htbin inside c:\KwickPython (like I mentioned above)
- Create a bat file and give a name, in my case I name it 'printService.bat'. Write this code:
c:\ cd c:\KwickPython python -m http.server --cgi 9100
- Put the printService.bat fie inside c:\KwickPython
- Open Run dialog with win+R and type 'shell:startup'. It will open startup directory, every program that you put inside this directory will be activated at the startup windows.
- Create a vbs file inside this directory. I name this file 'printService.vbs'. Write this code inside the file
Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\KwickPython\printService.bat" & Chr(34), 0
Set WinScriptHost = Nothing
That's it. Try to reboot your windows. After your windows finish booting, open cmd and check it using 'netstat -tan' whether port 9100 is already in Listening state. Try to print something. It works fine in my project.
I hope this can help you....
Langganan:
Postingan (Atom)