It is really easy to write a widget to the wordpress blog. So I thought of writing my own widget to show Google ads in my Blog. Here is how I did it.

  1. First generate the JavaScript code for your adsense account from the Google Adsense Page. You can do this by sigining in to the Dashboard of Adsense from https://www.google.com/adsense/. Then click the “Adsense Setup” tab and follow the wizard.
  2. Inside the “wp-content/plugins” directory of your Wordpress installation create a file for your plugin. (Say myadsense_widget.php)
  3. Then first write the code that should be appeared in your widget.  In this case you can just echo the the code provide by the Google. Anyway In order to make your widget complaint with the current theme, you have to use the code similar to the following.
    // the function for the widget
    function widget_myadsense($args) {
    	// being aware of the theme
    	extract($args);
    	echo $before_widget;
    	echo $before_title . "Google Adsense". $after_title;
    
    	// here you just echo the code provided by the google 
    	echo <<<GOOGLE_JS
    		<!-- in this space you have to copy paste
                    the code provided by the google-->
    GOOGLE_JS;
    
    	// again being aware of the theme
    	echo $after_widget;
    }
  4. Then write the code to register the above function as a widget with the following piece of code.
    // initiating widget
    function myadsense_init()
    {
    	register_sidebar_widget(__('My Adsense'), 'widget_myadsense');
    }
    
    // adding the action
    add_action("plugins_loaded", "myadsense_init");
  5. We are almost done here, But don’t forget you can mention your information as the widget plugin author with a comment similar to the following template.
    /*
    Plugin Name: MyAdsense
    Plugin URI: http://dimuthu.org
    Description: Adsense Plugin for my blog
    Author: Dimuthu Gamage
    Version: 0.1
    Author URI: http://dimuthu.org
    */
  6. That is all you have to code. Now just go the Plugins section of the wordpress from your Dashboard and enable the plugin (”myadsesne”) you just created.
  7. Go to the Design->Widget section and add your Widget to the Sidebar and click “Save Changes”. And go to your blog URL and make sure that the Ads are shown in there.

Last week I wrote a how to on Writing a SOAP and REST Service with PHP. It shows how to write a single script that enables the SOAP and REST web services interfaces using WSF/PHP. Today I’m going to show you how you can use WSF/PHP to write clients in both SOAP and REST form.

For that I use the Amazon Client Demo hosted in the PHP Web Services Demo Site. With this demo you can do shopping with Amazon using their web services API. It uses a php library (AmazonClient.php) that allows us to connect Amazon using SOAP or REST libraries.

You can see in the library code It is using different values for “useSOAP” argument depending on what form of messaging we like to use. Amazon SOAP service uses the SOAP 1.1 and the REST service uses the “GET” HTTP method for messaging. We can configure these details as mentioned in the following table.

REST SOAP
array(
      "to"=>self::AMAZON_REST_ENDPOINT,
      "HTTPMethod"=>"GET",
      "useSOAP" => FALSE)
array(
      "to"=>self::AMAZON_SOAP_ENDPOINT,
      "useSOAP" => "1.1",
      "action" => "http://soap.amazon.com")

Since Amazon requires different request messages for SOAP and REST services, we have to create them separately depending on the request type. But the response returns by the service is same in both cases so we can handle it using the same code.

Here is how ItemLookup operation of the Amazon service is written within the above mentioned constrains.

    /**
     * ItemLookup
     * @param $ASIN Amaxon Item Id
     * @return associate array consist of the response parameters
     */
    public function ItemLookup($ASIN)
    {

        if($this->is_soap)
        {
             $req_payload = <<<XML
                <ItemLookup xmlns="http://webservices.amazon.com/AWSECommerceService/2007-10-29">
                    <SubscriptionId>{$this->amazon_key}</SubscriptionId>
                    <ResponseGroup>Medium</ResponseGroup>
                    <Request>
                        <ItemId>{$ASIN}</ItemId>
                        <ReviewPage>1</ReviewPage>
                    </Request>
                </ItemLookup>
XML;
        }
        else
        {
             $req_payload = <<<XML
                <ItemLookup xmlns="http://webservices.amazon.com/AWSECommerceService/2007-10-29">
                    <SubscriptionId>{$this->amazon_key}</SubscriptionId>
                    <Service>AWSECommerceService</Service>
                    <ResponseGroup>Large</ResponseGroup>
                    <Operation>ItemLookup</Operation>
                    <ItemId>{$ASIN}</ItemId>
                    <ReviewPage>1</ReviewPage>
                </ItemLookup>
XML;
        }

        $ret_message = $this->request($req_payload);

        $simplexml = new SimpleXMLElement($ret_message->str);

        $res = $simplexml;

        return $res;
    }