Latest Entries »

The arc bobs against a god.

The arc bobs against a god.

We frequently need to grab data from remote site and there are several processes to do that. I liked process where we can garb using XML Path(xPath). XPath is used to navigate through elements and attributes in an XML document. XPath return a node result set to the calling method or application. A node is a complete element within an XML document.

Here is a process whether you can grab data from HTML/XHTML source using XPath and cURL.

The Basic Xpath Class:

Class xpath{
	public $html;
	public $patten;
	public $childnodes = 0;
	public $attribute = 0;
	public $forbidden = 0;
	public $search = 0;
	public $br2nl = 1;
	public $return = "string";
	public $charset = "utf8";
	private $retrund;
	
	function __construct($html, $patten){
		$this->html = $html; 
		$this->patten = $patten;
	}
	public function execute(){
		$xpath = new DOMXPath($this->html); 
		$basenodes = $xpath->query($this->patten);
		foreach ($basenodes as $basenode){
			if($this->childnodes){
				foreach($basenode->childNodes as $childnode){
					$this->buffer($childnode);
				}
			}else{
				$this->buffer($basenode);
			}
		}
	}
	private function buffer($value){
		$preparedvalue = $this->prepare($value);
		if($preparedvalue){
			if($this->returnstring()){ 
				$this->returnd .= $preparedvalue;
			}else{
				$this->returnd[] = $preparedvalue;
			}
		}
	}
	private function returnstring(){
		if($this->return == "string"){
			return true;
		}else{
			return false;
		}
	}
	private function returnutf8(){
		if($this->charset == "utf8"){
			return true;
		}else{
			return false;
		}
	}
	private function prepare($before){
		if($before->tagName == "br" AND $this->br2nl){
			$before = "\n";
		}else{
			if($this->attribute){
				if($this->search){
					if(in_array($before->textContent, $this->search)){
						$before = $before->getAttribute($this->attribute);	
					}else{
						$before = 0;
					}
				}else{
					$before = $before->getAttribute($this->attribute);
				}
			}else{
				if($this->search){
					if(in_array($before->textContent, $this->search)){
						$before = trim($before->textContent);
					}
				}else{
					$before = trim($before->textContent);
				}
			}
		}
		if(!$this->returnutf8()){
			$before = utf8_decode($before);
		}
		if($this->forbidden){
			if(in_array($before, $this->forbidden)){
				return false;
			}else{
				return $before;
			}
		}else{
			return $before;
		}
	}
	public function get(){
		return $this->returnd;
	}
}

We have to create xpath object to grab data using HTML ids/classes as nodes. At first, We need to grab page source using PHP cURL. When you have that source you can easily filter data using attributes/nodes.

Here is the example of fetching data using node and the process of reading HTML attributes.

	$grab_path = trim("Your URL goes here");
				
	$grab_path = str_replace("&","&",$grab_path);
	$url = urlencode($grab_path);
	$html = new DOMDocument(); 
	$html->loadHtmlFile($url); 
	
    $description = new xpath($html, '//div[@class="body_container"]'); // div id name
    $description->return = "string";
    $description->childnodes = 1;
    $description->forbidden = array("Body");
    $description->execute();

    $title = new xpath($html, '//h1'); //Grab h1 data 
    $title->execute();

    $image = new xpath($html, '//*[@id="main_image"]'); //Grab Images
    $image->attribute = "src";
    $image->execute();

    $adparams = new xpath($html, '//table[@class="params UnderlinedLinks"]/tr[1]/td[1]'); //fetch tables
    $adparams->childnodes = 1;
    $adparams->return = "array";
    $adparams->execute();

   $info[] = array(
		'title_name' => $title->get(), 
		'image' => $image->get(), 
	);

Its very effective to retrieve data from HTML source and its easily modifiable according to CSS and HTML changes.

Recently I faced a UNICODE encoding problem in MySQL Insert statement using PHP. When I tried to insert Swedish characters in MySQL table,the swedish characters ö, å, Ä were converted to ambiguous symbols because collation character set automatically converted to UTF8, usually I set MySQL collation into “utf8_general_ci”.

At last I find a solution in PHP to solve that problem, by using “mb_convert_encoding” we can convert encoding type at the insertion time.

$field = mb_convert_encoding($field,"iso-8859-1","utf-8");
	
@mysql_query("insert into table (field) values ('$field')");

This function will also work for other unicode character like spanish, Frecnch etc.

ভালবাসা এবং valentaine

ভালোবাসা শব্দটা মানুষের জীবনের সাথে যত মধুর ভাবে মিশে গেছে তা মনে হয় আর কোনো কিছই পারেনি.আবারও ভালবাসা দিবস চলে এসেছে কিন্তু ভালবাসার মোলায়েম আচর কি সবার জীবনে এসেছে.পৃথিবীর অনেক শব্দই তার জনপ্রিয়তা হারিয়েছে কিন্তু ভালবাসা শব্দটা পৃথিবীর শুরু থেকে শেষ পর্যন্ত যেন একইভাবে টিকে থাকবে.
ভালবাসা জিনিসটা আমার জীবনেও এসেছে অনেক বিচিত্র ভাবে, কিন্তু আমার এবারের ভালবাসার অনুভুতিটা একটু অন্যরকম আমি আমার জীবনে সত্যি যেন ভালবাসা কে অনুভব করছি, ভালবাসা দিয়ে আমার জীবন পরিপূর্ণ মনে করছি. দুরে থেকেও ভালবাসা যেন সব সময়ে আমার পাশে আছে. আমার এই ভালবাসার অনুভুতিটা আজ যেন প্রতিটি যুগল অনুভব করছে. সবার ভালবাসার দিনটা হোক ভালবাসার মতই সুন্দর. ওহ যারা এখন নিজের ভালবাসার মানুষকে খুঁজে পাননি তাদের জন্য বলছি. ভালবাসা নই শুধু যুগলদের, ভালবাসা তো সবার তাই সবাই কেই তার মাঝে ভালবাসা খুঁজে নিতে হবে. ত়া ছাড়া ভালবাসার নানা আয়োজনতো রয়েছেই
ফেব্র”য়ারি 14 শুধু যুগলরাই নয়, বিশ্ব ভালবাসা দিবসের আনন্দে সামিল হতে পারে সঙ্গীহীন তরুণ তরুণীরাও।হতাশা ও যন্ত্রণায় ডুবে না থেকে নিঃসঙ্গ মানুষরাও দিনটি উদযাপন করতে পারেন।
ভ্যালেন্টাইন’স ডে তে একা একা দিন না কাটিয়ে বন্ধুবিহীনরা যেন কোনো রেস্তোরাঁয় জড়ো হয়ে উৎসবে যোগ দিতে পারেন। কিংবা নিজের বাড়িতেও দিতে পারেন আন-ভ্যালেন্টাইন’স পার্টি।
নিঃসঙ্গদের উদ্দেশ্য করে বলছি ,”মজা করুন, কিংবা ধরে নেন যে মজাই করছেন। মানুষকে সঙ্গে নিয়ে হলেও একাকীত্বটুকুও উদযাপন করুন।”
তবে এটা শুধু কপোত কপোতি কিংবা দম্পতিদের জন্য নয়।মা বাবা ভাই বোন থেকে শুরু করে সবারই তো এই দিন.
আমার এই লেখাটা আজ সকল ভালবাসার মানুষদের জন্য যারা ভালবাসতে পছন্দ করে আর যারা ভালবাসা পেতে পছন্দ করে. ভালবাসার জয় হোক , ভালবাসা থাকুক সবার মাঝে.

Bread Crumb Component for CakePHP

Webpage Bread crumbing is common feature in web development. Breadcrumbs usually appear horizontally across the top of a web page, normally just below title bars or headers. BreadCrumb gives users a way to keep track of their locations within programs or documents. For using breadCrumb there must be a common component whether its can be effective in all pages through out the Site.
Here I show how to create and use a breadcrumb component in CakePHP.

<?php	
class BreadcrumbComponent extends Object
{
  var $controller;
  var $components = array('Session','Acl');

  function startup(&$controller)
  {
    $this->controller =& $controller; 
  }
    
	function setBreadcrumb($url)
	{
		$crumbs = split('/',$url);
		$link = '/';
		
		if($crumbs[0] != 'admin')
		{
			$breadcrumb[] = array('home', $link);
		}
		
		foreach($crumbs AS $crumb)
		{
			$name = str_replace('_',' ', $crumb);
			$link .= $crumb.'/';
			if($name && !is_numeric($name))
			{
				$breadcrumb[] = array($name,$link);
			}
			elseif(is_numeric($name))
			{
				$key = count($breadcrumb)-1;
				$breadcrumb[$key][1].= $name;
			}
		}
		
		return $breadcrumb;
	}
	
}
?>

Load the Component:

var $components = array( 'Breadcrumb');

Call from the controller:

function beforeRender()
	{
		$this->set('breadcrumb', $this->Breadcrumb->setBreadcrumb($this->params['url']['url']));
	}

This need to be modified according to sitemap

CodeIgniter has a nice helper class to download files. Its a nice way to download from server without any hassle just writing one single controller function.

public function download ($file_path = "")
	{
			$this->load->helper('download'); //load helper
			
			//$file_path = $this->input->post("file_path",TRUE);
			
			$layout="no_theme"; //if you have layout
			
			$data['download_path'] = $file_path;		
						
			$this->load->view("view file",$data);
			redirect("same url", "refresh");						
					
	}

In view section, just call the download function thats it.

	if( ! empty($download_path))
	{
		$data = file_get_contents(base_url() ."/...path.../".$download_path); // Read the file's contents
		$name = $download_path;

		force_download($name, $data);

	}

Its the most simple process to download using CodeIgniter

URL Shortening using bit.ly

The bit.ly API allows developers to interact in a programmatic way with the bit.ly website. The current API version is 2.0.1.
It’s a very simple way to shorten the URL through bit.ly API.

	/**
     * Function: URL_shortener
     *
     * @description : Create a shorten URl from a long URL
     * @param  $ : $url
     * @return : none
     * @author : sumon.
     * @last -modified-by: sumon.
     */

	function URL_shortener($url ="")
	{
		$api_data = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07");
		$array_data = json_decode($api_data);
		$result = $array_data->results;
		
		foreach($result as $results)
		{
			$short_url = $results->shortUrl;
		}
		return $short_url;
	}
	
	

It’s too simple to call that API.🙂

The YouTube Data API allows client applications to retrieve and update YouTube content in the form of Google Data API feeds. The client application can use the YouTube Data API to fetch video feeds, comments, responses, and playlists, as well as modify this information and to upload new video content to the site.

There is a awesome Client Library in PHP distributed by Zend is a part of zend framework which is very effective and powerful class to retrieve that from Google. This can be easily integrated with Codeigniter.
Here I share a Class which is very useful to Grad data from Youtube using Gdata class with Codeigniter.

<?php

class Youtube extends Controller
{
	public $layout="default";
	
	function __construct()
	{
		parent::Controller();
		$this->load->library("zend");
		$this->zend->load("Zend/Gdata/YouTube");
		
		$youtube = new Zend_Gdata_YouTube();
		$this->lang->load("resource");
		$this->load->helper("text");
	}

	function index()
	{
	
		//$youtube = new Zend_Gdata_YouTube();
		
		//Grab Video data
		$videoEntry = $youtube->getVideoEntry('video_code');
		
		//Grab Channel data
		$this->getAndPrintUserUploads("youtube_channel_name");
		
		$this->load->view('youtube_view', $data);
	}
	
	function getAndPrintUserUploads($userName)                    
	{     
		  $youtube = new Zend_Gdata_YouTube();
		  $youtube->setMajorProtocolVersion(2);
		  $this->printVideoFeed($youtube->getuserUploads($userName),$page=1,$page_counter=1);
	}  
		
	function printVideoFeed($videoFeed)
	{
		  $data['no_sidebar'] = true;
		  $count = 1;
		  
		  foreach ($videoFeed as $videoEntry) 
		  {
			
				//$this->printVideoEntry($videoEntry);
				$video_info[$count]['title'] = $videoEntry->getVideoTitle();
				$video_info[$count]['id']    = $videoEntry->getVideoId();
				$video_info[$count]['desc']  = $videoEntry->getVideoDescription();
				$video_info[$count]['tags']  = implode(", ", $videoEntry->getVideoTags());
				$video_info[$count]['updated']  = $videoEntry->getUpdated();
				$video_info[$count]['category']  = $videoEntry->getVideoCategory();				
				
				$count++;
		  }
		//pre($video_info);
		
		$data['video_info'] = $video_info;
		$this->load->view("resources/youtube_list_view",$data);
	}
	
		
	
	function printVideoEntry($videoEntry) 
	{
	  // the videoEntry object contains many helper functions
	  // that access the underlying mediaGroup object
	  echo 'Video: ' . $videoEntry->getVideoTitle() . "<br>";
	  echo 'Video ID: ' . $videoEntry->getVideoId() . "<br>";
	  echo 'Updated: ' . $videoEntry->getUpdated() . "<br>";
	  echo 'Description: ' . $videoEntry->getVideoDescription() . "<br>";
	  echo 'Category: ' . $videoEntry->getVideoCategory() . "<br>";
	  echo 'Tags: ' . implode(", ", $videoEntry->getVideoTags()) . "<br>";
	  echo 'Watch page: ' . $videoEntry->getVideoWatchPageUrl() . "<br>";
	  echo 'Flash Player Url: ' . $videoEntry->getFlashPlayerUrl() . "<br>";
	  echo 'Duration: ' . $videoEntry->getVideoDuration() . "<br>";
	  echo 'View count: ' . $videoEntry->getVideoViewCount() . "<br>";
	  echo 'Rating: ' . $videoEntry->getVideoRatingInfo() . "<br>";
	  echo 'Geo Location: ' . $videoEntry->getVideoGeoLocation() . "<br>";
	  echo 'Recorded on: ' . $videoEntry->getVideoRecorded() . "<br>";
	  
	  // see the paragraph above this function for more information on the 
	  // 'mediaGroup' object. in the following code, we use the mediaGroup
	  // object directly to retrieve its 'Mobile RSTP link' child
	  foreach ($videoEntry->mediaGroup->content as $content) {
		if ($content->type === "video/3gpp") {
		  echo 'Mobile RTSP link: ' . $content->url . "<br>";
		}
	  }
  
  echo "Thumbnails:<br>";
  $videoThumbnails = $videoEntry->getVideoThumbnails();

		  foreach($videoThumbnails as $videoThumbnail) {
			echo $videoThumbnail['time'] . ' - ' . $videoThumbnail['url'];
			echo ' height=' . $videoThumbnail['height'];
			echo ' width=' . $videoThumbnail['width'] . "<br>";
		  }
	}

}

?>

Zend provides some cool library classes for Youtube.

There is another way to grab youtube data from youtube Channel that is grabbing from API feed. Which is another easy process.


/**
 * Function: youtube data grabber
 *
 * @description :
 * @param  $ : video code, url type (embed/url)
 * @return : data array
 * @author : Mamun.
 * @last -modified-by: Mamun.
 */
if (! function_exists('youtube_data_grabber' ))
{

		function youtube_data_grabber($video_code, $link_type = "embed")
		{
				if ($video_code != '')
				{
					if ($link_type == "embed")
					{
						$splited_data = explode("=",$video_code);
						$video_unique_code = substr(strrchr($splited_data[4],"/"),1,-strlen(strrchr($splited_data[4],"&")));

					}
					else if ($link_type == "url")
					{
						$splited_data = explode("=",$video_code);
						$video_unique_code = substr($splited_data[1],0,-strlen(strrchr($splited_data[1],"&")));
					}
					else
					{
						return;
					}

						// set feed URL
						$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$video_unique_code;

						// read feed into SimpleXML object
						$sxml = simplexml_load_file($feedURL);

					return $sxml;
				}

		}
}

You Tube provides two types (small/large) of thumbnails for each video. It quite simple task to grab thumbnail from YouTube video. I created this function for grabbing thumbnails form YouTube videos. Its able to grab from both URL and Embedded code and also small and large size.

Function Body:

/**
* Function: youtube thumb grabber
*
* @description :
* @param  $ : video code, url type (embed/url),size (small/Large),thumb link
* @return : thumb path
* @author : Mamun.
* @last -modified-by: Mamun.
*/
if (! function_exists('youtube_thumb_grabber' ))
{

     function youtube_thumb_grabber($video_code, $link_type = "embed", $size = "small", $thumb_link = "")
    {
            if ($video_code != '')
            {
                   if ($link_type == "embed")
                   {

                                  $splited_data = explode("=",$video_code);

                                  $video_unique_code = substr(strrchr($splited_data[4],"/"),1,-strlen(strrchr($splited_data[4],"&")));

                  }
                  else if ($link_type == "url")
                  {
                                  $splited_data = explode("=",$video_code);
                                  $video_unique_code = substr($splited_data[1],0,-strlen(strrchr($splited_data[1],"&")));
                  }
                  else
                  {
                                  return;
                  }

                  if($size == "small")
                  {
                            return "<a href=\"$thumb_link\"><img src=\"http://img.youtube.com/vi/$video_unique_code/2.jpg\" alt=\"No image\" /></a>";
                  }
                  else if ($size == "large")
                  {
                            return "<a href=\"$thumb_link\"><img src=\"http://img.youtube.com/vi/$video_unique_code/0.jpg\" alt=\"No image\" /></a>";
                  }
                  else
                  {
                            return "<a href=\"$thumb_link\"><img src=\"http://img.youtube.com/vi/$video_unique_code/2.jpg\" alt=\"No image\" /></a>";

                   }

             }

      }
}

Calling Function:

<?php 

echo youtube_thumb_grabber($video["video_code"],"embed","small","resources/videos/view/".$video["video_title_url"]);

?>

Its Cool.😀

In all Entertainment sites, Star rating is a common phenomena. We need to use star rating for rating different resources like videos, articles, thesis, posts even sometimes persons. JQuery has an excellent Star Rating plugin, which I recently use with PHP CodeIgniter. This is very simple to integrate with controller and show high quality rating options.

Jquery Call:


$(function() {
       $('.auto-submit-star').rating({
            required: true,
            callback: function(value, link) {

            $.ajax({
                     type: "post",
                     url: site_url + "user/view/star",
                     dataType: "json",
                     data: "&video=" + $('#video_url').val() + "&rate_val=" + value,

              success: function(e) {
                   $.jGrowl(e.code + "<br>" + e.msg);
               }
         });
     }
});

Controller Method:

public function star()
    {
        $rate = $this->input->post("rate_val", true);
        $video_url = $this->input->post("video", true);
        $this->load->model("Model Name");
        $video_id = $this->model_name->get_video_id($video_url);
        unset($this->layout); //Block template Layout
        if (get_user_id())   //get_user_id() return login user id
        {
            if (!$this->model_name->is_video_rated(get_user_id(), $video_id))
            {
                $data = array("video_id" => $video_id,
                    "user_id" => get_user_id(),
                    "videos_rating_value" => $rate,
                    "videos_rating_date" => "" . date("Y-m-d H:i:s") . ""
                    );
                if ($this->model_name->insert_rating($data, $video_id, $rate))
                {
                    echo json_encode(array("code" => "Success", "msg" => "Your Video Rating has been posted"));
                }
                else
                {
                    echo json_encode(array("code" => "Error", "msg" => "There was a problem rating your video"));
                }
            }
            else
            {
                echo json_encode(array("code" => "Error", "msg" => "You have already rated this video"));
            }
        }
        else
        {
            echo json_encode(array("code" => "Error", "msg" => "You have to login to rate the video"));
        }
        exit(0);
    }

Presentation View:

    if (!get_user_id()) //Check if user logged in
    {
        $radio_level = "disabled";
    }
    else
    {
        $radio_level = " ";
    }

    for($i = 1;$i <= 5;$i++)
    {
        if ($i == round($row["total_rate"]))
        {
       ?>
       <input class="auto-submit-star" type="radio" name="rating" <?php echo "$radio_level";

            ?> value="<?php echo $i;

            ?>" checked="checked"/>
	<?php
        }
        else
        {
         ?>
	<input class="auto-submit-star" type="radio" name="rating" <?php echo "$radio_level";

            ?> value="<?php echo $i;

            ?>"/>
   <?php
        }
    } //end of for
?>
    

Its a very cool🙂 rating JQuery plugins.
For demo click

Follow

Get every new post delivered to your Inbox.