Wednesday, August 3, 2011

Amazon Product Adevetising API Code

Example to access the Amazon Product Advertising API using the provided class.
<?php

    /* Example usage of the Amazon Product Advertising API */
    include("amazon_api_class.php");

    $obj = new AmazonProductAPI();

    try
    {
        /* Returns a SimpleXML object */
         $result = $obj->searchProducts("Akon",
                                       AmazonProductAPI::Music,
                                       "TITLE");
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }

    print_r($result);



foreach($result->Items->Item as $itm)
{
       $SearchIndex1=$_REQUEST['SearchIndex'];
      $asin=$itm->ASIN;
     $SearchIndex=$itm->ItemAttributes->Title;

     echo  $itm->ItemAttributes->Title; 
}

?>
You can convert the returned xml to json with the following line.

$result = json_encode($result);


Api Access class are

<?php
 
require_once 'aws_signed_request.php';
 
class AmazonProductAPI
{
 
    private $public_key     = "YOUR AMAZON ACCESS KEY ID";
    private $private_key    = "YOUR AMAZON SECRET KEY";
 
    /* 'Associate Tag' now required, effective from 25th Oct. 2011 */
    private $associate_tag  = "YOUR AMAZON ASSOCIATE TAG";
 
    const MUSIC = "Music";
    const DVD   = "DVD";
    const GAMES = "VideoGames";
 
    private function verifyXmlResponse($response)
    {
        if ($response === False)
        {
            throw new Exception("Could not connect to Amazon");
        }
        else
        {
            if (isset($response->Items->Item->ItemAttributes->Title))
            {
                return ($response);
            }
            else
            {
                throw new Exception("Invalid xml response.");
            }
        }
    }
 
    private function queryAmazon($parameters)
    {
        return aws_signed_request("com",
                                  $parameters,
                                  $this->public_key,
                                  $this->private_key,
                                  $this->associate_tag);
    }
 
    public function searchProducts($search,$category,$searchType="UPC")
    {
        $allowedTypes = array("UPC", "TITLE", "ARTIST", "KEYWORD");
        $allowedCategories = array("Music", "DVD", "VideoGames");
 
        switch($searchType) 
        {
            case "UPC" :
                $parameters = array("Operation"     => "ItemLookup",
                                    "ItemId"        => $search,
                                    "SearchIndex"   => $category,
                                    "IdType"        => "UPC",
                                    "ResponseGroup" => "Medium");
                            break;
 
            case "TITLE" :
                $parameters = array("Operation"     => "ItemSearch",
                                    "Title"         => $search,
                                    "SearchIndex"   => $category,
                                    "ResponseGroup" => "Medium");
                            break;
 
        }
 
        $xml_response = $this->queryAmazon($parameters);
 
        return $this->verifyXmlResponse($xml_response);
 
    }
 
    public function getItemByUpc($upc_code, $product_type)
    {
        $parameters = array("Operation"     => "ItemLookup",
                            "ItemId"        => $upc_code,
                            "SearchIndex"   => $product_type,
                            "IdType"        => "UPC",
                            "ResponseGroup" => "Medium");
 
        $xml_response = $this->queryAmazon($parameters);
 
        return $this->verifyXmlResponse($xml_response);
 
    }
 
    public function getItemByAsin($asin_code)
    {
        $parameters = array("Operation"     => "ItemLookup",
                            "ItemId"        => $asin_code,
                            "ResponseGroup" => "Medium");
 
        $xml_response = $this->queryAmazon($parameters);
 
        return $this->verifyXmlResponse($xml_response);
    }
 
    public function getItemByKeyword($keyword, $product_type)
    {
        $parameters = array("Operation"   => "ItemSearch",
                            "Keywords"    => $keyword,
                            "SearchIndex" => $product_type);
 
        $xml_response = $this->queryAmazon($parameters);
 
        return $this->verifyXmlResponse($xml_response);
    }
 
}
 
?>

Amazon Signed Request

<?php
 
function  aws_signed_request($region,
                             $params,
                             $public_key,
                             $private_key,
                             $associate_tag)
{
 
    $method = "GET";
    $host = "ecs.amazonaws.".$region;
    $uri = "/onca/xml";
 
 
    $params["Service"]          = "AWSECommerceService";
    $params["AWSAccessKeyId"]   = $public_key;
    $params["AssociateTag"]     = $associate_tag;
 
    $params["Timestamp"]        = gmdate("Y-m-d\TH:i:s\Z");
    $params["Version"]          = "2009-03-31";
 
    /* The params need to be sorted by the key, as Amazon does this at
      their end and then generates the hash of the same. If the params
      are not in order then the generated hash will be different from
      Amazon thus failing the authentication process.
    */
    ksort($params);
 
    $canonicalized_query = array();
 
    foreach ($params as $param=>$value)
    {
        $param = str_replace("%7E", "~", rawurlencode($param));
        $value = str_replace("%7E", "~", rawurlencode($value));
        $canonicalized_query[] = $param."=".$value;
    }
 
    $canonicalized_query = implode("&", $canonicalized_query);
 
    $string_to_sign = $method."\n".$host."\n".$uri."\n".
                            $canonicalized_query;
 
    /* calculate the signature using HMAC, SHA256 and base64-encoding */
    $signature = base64_encode(hash_hmac("sha256", 
                                  $string_to_sign, $private_key, True));
 
    /* encode the signature for the request */
    $signature = str_replace("%7E", "~", rawurlencode($signature));
 
    /* create request */
    $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
 
    /* I prefer using CURL */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$request);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 
    $xml_response = curl_exec($ch);
 
    if ($xml_response === False)
    {
        return False;
    }
    else
    {
        /* parse XML and return a SimpleXML object, if you would
           rather like raw xml then just return the $xml_response.
         */
        $parsed_xml = @simplexml_load_string($xml_response);
        return ($parsed_xml === False) ? False : $parsed_xml;
    }
}
?>





Monday, May 9, 2011

To add Two number by useing classes in php

<?php
class abc
{
   
   
 function addition ($a=0,$b=0)
 {
    
   
    return ($a + $b);  
 }

 
 
 
 function multi ($a=1, $b=1)
 {
   

  
    return ($a * $b);  
 }



 function div($a=1 , $b=1 )
 {
  
    return ($a / $b);
 }

 }

$a=new abc;
//echo $a->addition($value1 + $value2);
?>

<html>

<body>
<form method="post">
<input type="text" name="value1" value="0" />
<input type="text" name="value2" value="0" />
<input type="submit" name="submit" value="Sum" /><br />
addtion<input type="text" value="<?php echo $a->addition($_POST['value1'] + $_POST['value2']) ?>" /><br />
multiplication<input type="text" value="<?php echo $a->multi($_POST['value1'] * $_POST['value2']) ?>" /><br />
Division<input type="text" value="<?php echo $a->multi($_POST['value1']/$_POST['value2']) ?>" /><br/>
</form>
</body>
</html>

how to make config file

 http://www.hotscripts.com/category/php/

<?php  $dbHost = 'localhost';
    $dbUser = 'root';
    $dbPass = '';
    $dbName = 'vedemo1';
    $con = mysql_connect($dbHost, $dbUser, $dbPass, $dbName);
    mysql_select_db("vedemo1",$con);  
?>

Sunday, May 8, 2011

How to make a custom Simple module to show your static data in Drupal



Drupal is totally based  on module   I am also  studying module and trying to implement module .
 Here  I am describing the  custom module how to show your  data by module .
 For this you first have to make the  the  file  for module  info  .  I  am  making  it  by my name   vikas.info 

And put your module information on it by the following format

;$Id$
name = "Vikas  module"
description = "this is fist vikas fisrst module"
core = 6.x
php = 5.1

Remember  always  save your custom  module in  site/all/module/

After making  vikas.info  file  save it on above location in folder vikas
Now you have to  make  file for the module  data  now  name the new file
As  vikas.module   and put the following data  in it

<?php
// $Id$
/**
 * @file
 * Module for fetching data from Goodreads.com.
 * This module provides block content retrieved from a
 * Goodreads.com bookshelf.
 * @see http://www.goodreads.com
 */
 /**
 * Implementation of hook_block()
 */
function vikas_block($op='list', $delta=0, $edit=array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Goodreads Bookshelf');
      return $blocks;
    case 'view':
      $blocks['subject'] = t('vikas');
      $blocks['content'] = t('hello  world ');
      return $blocks;
  }
}
Now   you  static module  I studied it from book and  for good read so not  changing the info
 Now go to your drupal admin  panel   and activate the module and from the block select the option for the  block where you want to display   .this module is compatible with drupal 6.x version