Sending Push notification through pushwoosh from php

[php] $push_auth = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’;
$push_app_id = ‘XXXXX-XXXXX’;
$push_debug = false;
$title = ”; // pushwoosh title
$banner = ”; // pushwoosh banner
$send_date = ‘now’; // pushwoosh date
$android_header = ”; // pushwoosh android header
$android_custom_icon = ” pushwoosh notification icon;
sendpush(‘createMessage’, array(
‘application’ => $push_app_id,
‘auth’ => $push_auth,
‘notifications’ => array(
array(
‘send_date’ => $send_date,
‘content’ => $title,
‘android_header’=>$android_header,
‘android_custom_icon’ =>$android_custom_icon,
‘android_badges’ => 2,
‘android_vibration’ => 1,
‘android_priority’ => 1,
‘data’ => array(‘custom’ => ‘json data’),
),
)
));

function sendpush($method, $data) {
$url = ‘https://cp.pushwoosh.com/json/1.3/’ . $method;
$request = json_encode([‘request’ => $data]);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, ‘gzip, deflate’);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

if (defined(‘PW_DEBUG’) && self::$push_de) {
print "[PW] request: $request\n";
print "[PW] response: $response\n";
print "[PW] info: " . print_r($info, true);
}

return $info;
}
}

[/php]

Example

Display image in grid view YII2

In Yii 2 there are many function including built-in helper for building urls. So we can easily bulld the url to image by path too .

To display the image in grid view :

[php]
[
‘attribute’ => ‘image’,
‘format’ => ‘html’,
‘value’ => function($data) {
return Html::img(\Yii::$app->request->BaseUrl.’/uploads/’.$data->image,[‘width’=>100]);
},
]
[/php]

To display the image in DetailView :
[php]
[
‘attribute’=>’image’,
‘value’=>(\Yii::$app->request->BaseUrl.’/uploads/tips/’.$model->image),
//’value’=>$model->image,
‘format’ => [‘image’,[‘width’=>’100′,’height’=>’100’]],
],
[/php]

Install yii2 advanced template using composer in windows

The minimum requirement for YII2 is that your Web server supports PHP 5.4.0.

The installation require following steps:

1: If you don’t have composer, download it from https://getcomposer.org/Composer-Setup.exe and execulete in yout root folder like c:\xampp\php\
2: Go to your project folder like c:\xampp\htdocs\yourprojectfolder
[php]
> c:\xampp\htdocs\yourprojectfolder>composer global require "fxp/composer-asset-plugin:^1.2.0"
> c:\xampp\htdocs\yourprojectfolder>composer create-project –prefer-dist –stability=dev yiisoft/yii2-app-advanced[/php]

3. After this run php init command it is just copy files from directory /environments to your project
[php]> c:\xampp\htdocs\yourprojectfolder>php init[/php]

Regular expression for decimals as well as integers with a range limit

This is an example for regular expression which allow both decimals as well as integers with a range limit of 40-180
Regex Pattern:
[php]
/^([4-8][0-9]|[4-8][0-9]\d{0,9}(\.\d{1,3})|9[0-9]\d{0,9}(\.\d{1,3})|1[0-7][0-9]|1[0-7][0-9]\d{0,9}(\.\d{1,3})|180)$/
[/php]

  • [4-8][0-9] : Parse Into Range 40 – 89
  • [4-8][0-9]\d{0,9}(\.\d{1,3}) : Parse Into Range 40.00 – 89.00
  • 9[0-9] : Parse Into Range- 90 – 99
  • 9[0-9]\d{0,9}(\.\d{1,3}) : Parse Into Range- 90.00 – 99.00 with three decimal places
  • 1[0-7][0-9] : Parse Into Range- 100 – 179
  • 1[0-7][0-9]\d{0,9}(\.\d{1,3} : Parse Into Range- 90.00 – 99.00 with three decimal places
  • 180 : Parse Into Range – 180-180

Excel reader in YII2

PHPExcel extension is used to experting and importing Excel to PHP. In this example we are reading Excel data by using this extension and inserting the data in given tables accordingly.

PHPExcel extension is used to experting and importing Excel to PHP. In this example we are reading Excel data by using this extension and inserting the data in given tables accordingly.

Step 1: Installation : Download the extension from https://github.com/PHPOffice/PHPExcel and put it in vendor directory. Then update your composer or add the following code in your composer.jason file under require array().
code: “phpoffice/phpexcel”: “dev-develop” Download the extension from https://github.com/PHPOffice/PHPExcel and put it in vendor directory. Then update your composer or add the following code in your composer.jason file under require array().
[php]code: "phpoffice/phpexcel": "dev-develop"[/php]
step 2: In your model :
[php]public $files;
public static function tableName()
{
return ‘tbl’;
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[[‘files’], ‘file’, ‘skipOnEmpty’ => true, ‘extensions’ => ‘csv, xlsx,xls’, ‘maxFiles’ => 1],
];
}
[/php]
Step 3: In your controller
[php] public function actionImport()
{
$objPHPExcel = new \PHPExcel();
if (Yii::$app->request->isPost) {
$excel_files = UploadedFile::getInstance($model, ‘files’);
$objPHPExcel = \PHPExcel_IOFactory::load($excel_files->tempName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
if(isset($sheetData)){
foreach($sheetData as $row){
/*Your code*/
}
}
}
}
[/php]
Step 3: In your View
[php]<?php $form = ActiveForm::begin([‘options’ => [‘enctype’ => ‘multipart/form-data’]]) ?>
<?= $form->field($model, ‘files’)->fileInput() ?>
<?php echo Html::submitButton($model->isNewRecord ? ‘Upload’ : ‘Update’, [
‘class’=>$model->isNewRecord ? ‘btn btn-success’ : ‘btn btn-primary’]
);?>
<?php ActiveForm::end() ?>
[/php]

HTTP basic authentication using CURL PHP

CURL is a library that allows you to connect and communicate to many different types of servers with many different types of protocols. Or wecan say CURl is library through which we can make HTTP request in PHP.

In order to use CURL funcion you need to install the libcurl package.
The basic steps of using CURl function in PHP are:

1) Initialize a curl session i.e. (curl_init())
2) Set various options for the session i.e.(curl_setopt()).
3) Execute and fetch/send data from/to server i.e.(curl_exec())
4) Close the session i.e. (curl_close())
Example:
[php]

$request = ‘req’;
$u_id = 12345;
$webkey = ‘asddAA12hhsh’;
param = array(‘request’=>$request,’uid’=>$u_id,’webkey’=>$webkey);
$cookies = ‘CookieName1=Value;CookieName2=Value’;
$curl = ‘https://www.localhost/abc’;
$data_string = json_encode($param);
curl_setopt($ch, CURLOPT_URL,$curl);

curl_setopt($ch, CURLOPT_COOKIE, $cookies);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

$output = @curl_exec($ch); // execute
$record = json_decode($output, true);
if(curl_errno($ch)){
echo ‘Request Error:’ . curl_error($ch);
}
else{
curl_close($ch);
print_r($record);
}
[/php]

Google Maps – More Controls

Google Maps – More Controls

In addition to the default controls, Google Maps also has:

  • Scale – displays a map scale element
  • Rotate – displays a small circular icon which allows you to rotate maps
  • Overview Map – displays a thumbnail overview map reflecting the current map viewport within a wider area

You can specify which controls to show when creating the map (inside MapOptions) or by callingSETOPTIONS() to change the map’s options.

 

The Default Controls – Google Map

Google Maps – The Default Controls

When showing a standard Google map, it comes with the default control set:

  • Zoom – displays a slider or “+/-” buttons to control the zoom level of the map
  • Pan – displays a pan control for panning the map
  • MapType – lets the user toggle between map types (roadmap and satellite)
  • Street View – displays a Pegman icon which can be dragged to the map to enable Street View

SVG (Scalable Vector Graphics)

What is SVG?

  • SVG stands for Scalable Vector Graphics
  • SVG is used to define vector-based graphics for the Web
  • SVG defines the graphics in XML format
  • SVG graphics do NOT lose any quality if they are zoomed or resized
  • Every element and every attribute in SVG files can be animated
  • SVG is a W3C recommendation
  • SVG integrates with other W3C standards such as the DOM and XSL

SVG is a W3C Recommendation

SVG 1.0 became a W3C Recommendation on 4 September 2001.

SVG 1.1 became a W3C Recommendation on 14 January 2003.

SVG 1.1 (Second Edition) became a W3C Recommendation on 16 August 2011.

SVG Advantages

Advantages of using SVG over other image formats (like JPEG and GIF) are:

  • SVG images can be created and edited with any text editor
  • SVG images can be searched, indexed, scripted, and compressed
  • SVG images are scalable
  • SVG images can be printed with high quality at any resolution
  • SVG images are zoomable (and the image can be zoomed without degradation)
  • SVG is an open standard
  • SVG files are pure XML

The main competitor to SVG is Flash.

The biggest advantage SVG has over Flash is the compliance with other standards (e.g. XSL and the DOM). Flash relies on proprietary technology that is not open source.