Thursday, October 24, 2013

Create Dynamic Multidimensional File Tree Array

This is another my PHP snippet to create file tree array dynamically.

<?php
function wps_glob($dir) {
  foreach (glob($dir . '/*') as $f) {
    if(is_dir($f)) {
      $r[] = array(basename($f) => wps_glob($f));
    }
    else {
      $r[] = basename($f);
    }
  }
  return $r;
}

function wps_files($path) {
  $wpsdir = Array(
     'root' => $path,
     'struktur' =>  wps_glob($path)
  );
  return $wpsdir;
}
?>

Usage

<?php

echo '<pre>';
print_r( wps_files( PATH_TO_YOUR_DIRECTORY ) );
echo '</pre>';

?>

PHP Function to send post using CURL

This is one of my PHP snippet collection. This function can performs post request with parameters set. I have used this function for sending request to several API servers and so far it works as well as I need. May be you need to add curl_setopt based on your needs.

The Snippet:
<?php 
function curl_post($url,$params) {
  $parse_url = parse_url($url);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_TIMEOUT,60);
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  if($parse_url['scheme'] == 'https') {
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  }
  $result = curl_exec($ch);
  curl_close ($ch);
  return $result;
}
?>

Usage

Below is just example usage to work with API service.

<?php
$url = 'https://api.server.example.com/post.php';
 
// you need to set post parameters
$params = Array(
    'app_id'       => $app_id,
    'app_secret'   => $app_secret,
    'message'      => $message
);

$result = curl_post($url,$params);

?>

Monday, October 21, 2013

How to Include All PHP Files in Directory - The Easy Way

Ever wonder how to include all php files in directory? I just created PHP snippets that may be useful for you. Here is the code:

The contents of include.php:
<?php
define('MYBASEPATH' , 'inc/');
foreach (glob(MYBASEPATH . '*.php') as $file) {
    if($file != 'index.php') {
        include($file);
    }
}
?>

The code above will include all files in directory inc except index.php. To include all files, simply include include.php file on your script.

<?php
include( 'include.php' );
?>

Enjoy :-)

How to create mysql partitions

The website become very slow because there are too much data on mysql tables. This is the common problem for sites which save historical data. I just found this issue on my website, then googling to find solutions for this. And then, yap, there's simple solution for handling large table issues.

Sunday, October 13, 2013

Creating Simple jQuery QR Code Generator

I've created simple qr code generator using jquery and google infographics (deprecated) chart API. This time I'd like to share this simple and easy trick.

The Demo

Image Result

Download Source Code


HTML Elements

The element to show Qr Code image after jquery event. Below is the example:
<div id="outwrap">

      <div class="left" style="width:490px">
          <textarea id="qrc" style="width:490px;height:220px"></textarea>
          <button id="buildqr">Create</button>
          <span id="qrerror" class="error"></span>
      </div>
      <div class="right" class="tbl" style="width:250px"><div class="tbl-cell" id="qresult">Image Result</div></div>
      <div class="clear"></div>

</div>

Sunday, October 6, 2013

Block temporary emails using PHP

It's really sucks when someone registers on site using temporary email address. This is not good for business. Especially if you pay online services to handle your email marketing campaign. That's why you need securing your registration form not just for email validation, but also prevent receiving contacts that you will never be able to follow up them. I did this by filtering inputs and detect email domain to make sure it isn't in temporary email service list.

Here's how: