User Tools

Site Tools


podoc:webservice

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
podoc:webservice [2007/04/13 14:19] – created Luudpodoc:webservice [2007/04/13 21:13] (current) Luud
Line 1: Line 1:
 ===== Photo Organizer Webservice ===== ===== Photo Organizer Webservice =====
 +
 +By Luud Heck, April 2007
 +
 +** NOTE: at this moment this page is little more than a dump of some things I've tested and proven to work. I have made further advances already and will update this page as soon as possible. **
  
 ==== Introduction ==== ==== Introduction ====
Line 6: Line 10:
  
 Below I explain how I publish my websites with MediaWiki and include Photo Organizer photos and albums in my wiki pages. The crux is that the information about photos and albums will be provided by Photo Organiser as a SOAP based webservice. Below I explain how I publish my websites with MediaWiki and include Photo Organizer photos and albums in my wiki pages. The crux is that the information about photos and albums will be provided by Photo Organiser as a SOAP based webservice.
 +
  
 ==== MediaWiki ==== ==== MediaWiki ====
Line 242: Line 247:
 Here is the CSS file that needs to be imported in the wiki skin CSS file (use the import directive and place it at the start of the main.css file in the monobook skin for example). Here is the CSS file that needs to be imported in the wiki skin CSS file (use the import directive and place it at the start of the main.css file in the monobook skin for example).
 <code> <code>
-content still needs to be entered+/* 
 + * Photo Organizer Extension Styling 
 + */ 
 + 
 +/* 
 + * Do not float the album container, but keep it an inline element! 
 + */ 
 +div.po_album { 
 +  float: none; 
 +  width: 100%; 
 +  /* background-color: yellow; 
 +  border: 1px solid red; */ 
 +  clear:both; 
 +
 + 
 +.po_thumbnail { 
 +  margin: 0px; 
 +  padding: 0px; 
 +
 + 
 +/* 
 + * Make sure to float the thumnails (left or right). 
 + */ 
 +div.po_thumbnail { 
 +  float: left; 
 +  border: 0px solid gray; 
 +  padding: 3px; 
 +  margin: 3px; 
 +
 + 
 +table.po_thumbnail, tr.po_thumnail, td.po_thumnail { 
 +  /* border: 1px solid red; */ 
 +  border: 0px; 
 +  padding: 0px; 
 +  margin: 0px; 
 +  width: 100%; 
 +  height: 100%; 
 +
 + 
 +td.po_thumbnail { 
 +  text-align: center; 
 +  vertical-align: middle; 
 +
 + 
 +img.po_thumbnail { 
 +  border: 1px solid black; 
 +}
 </code> </code>
 +
 +
 +==== Photo Organizer SOAP interface ====
 +
 +Having the basics of creating a MediaWiki extension for Photo Organizer working, lets look at how we can make this extension retrieve information from the Photo Organizer database.
 +
 +=== PHP-SOAP ===
 +
 +Recent versions of PHP include good support for the SOAP protocol in the form of the [[http://www.php.net/manual/en/ref.soap.php|PHP-SOAP]] extension of PHP.
 +
 +Now, before you start experimenting, take this TIP and follow it!
 +
 +**Make sure to disable WSDL caching in both client and server, and check every time that it is still there! **
 +
 +<code>ini_set('soap.wsdl_cache_enabled', '0');</code>
 +
 +I thought I had it disabled, but trying things and starting over once in a while resulted in not having WSDL caching disabled in my client. This oversight has cost me a couple of evenings getting thing to work right.
 +
 +Next tip:
 +
 +** Validate your WSDL file! **
 +
 +If you have Mono installed on your Linux box you can use the wsdl command.
 +
 +Here is an example static serverice that generates some kind of Photo Organizer album information. This is just a demonstration, not a functional solution for PO yet. Just put in in a directory wsdl-po on your PHP with SOAP enabled webserver and it should work if you browse to the client.php file.
 +
 +photoorganizer.wsdl
 +<code>
 +<?xml version='1.0' encoding='UTF-8' ?>
 +<definitions name='PhotoOrganizer'
 +    targetNamespace='urn:PhotoOrganizer'
 +    xmlns='http://schemas.xmlsoap.org/wsdl/'
 +    xmlns:tns='urn:PhotoOrganizer'
 +    xmlns:xsd='http://www.w3.org/2001/XMLSchema'
 +    xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
 +    xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
 +    xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'>
 +
 +    <types xmlns='http://schemas.xmlsoap.org/wsdl/'>
 +        <schema xmlns='http://www.w3.org/2001/XMLSchema'
 +            targetNamespace='urn:PhotoOrganizer'>
 +
 +            <complexType name="photoInfo">
 +                <all>
 +                    <element name="id" type="xsd:int"/>
 +                    <element name='title' type='xsd:string'/>
 +                </all>
 +            </complexType>
 +
 +            <complexType name="photoAlbum">
 +                <complexContent>
 +                    <restriction base="soapenc:Array">
 +                        <attribute
 +                            ref="soapenc:arrayType"
 +                            wsdl:arrayType="tns:photoInfo[]"/>
 +                    </restriction>
 +                </complexContent>
 +            </complexType>
 +
 +        </schema>
 +    </types>
 +
 +    <message name='getAlbumRequest'>
 +        <part name='id' type='xsd:int'/>
 +    </message>
 +
 +    <message name='getAlbumResponse'>
 +        <part name='album' type='tns:photoAlbum'/>
 +    </message>
 +
 +    <portType name='PhotoOrganizerPort'>
 +        <operation name='getAlbum'>
 +            <input message='tns:getAlbumRequest'/>
 +            <output message='tns:getAlbumResponse'/>
 +        </operation>
 +    </portType>
 +
 +    <binding name='PhotoOrganizerBinding' type='tns:PhotoOrganizerPort'>
 +        <soap:binding style='rpc'
 +            transport='http://schemas.xmlsoap.org/soap/http'/>
 +        <operation name='getAlbum'>
 +            <soap:operation soapAction='urn:PhotoOrganizer#getAlbum'/>
 +            <input>
 +                <soap:body namespace='urn:PhotoOrganizer'
 +                    use='encoded'
 +                    encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
 +            </input>
 +            <output>
 +                <soap:body namespace='urn:PhotoOrganizer'
 +                    use='encoded'
 +                    encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
 +            </output>
 +        </operation>
 +    </binding>
 +
 +    <service name='PhotoOrganizerService'>
 +        <port name='PhotoOrganizerPort' binding='tns:PhotoOrganizerBinding'>
 +            <soap:address location='http://localhost/wsdl-po/server.php'/>
 +        </port>
 +    </service>
 +
 +</definitions>
 +</code>
 +
 +common.php
 +<code>
 +<?php
 +
 +$wsdlfile = "photoorganizer.wsdl";
 +
 +$photoClassMap = array (
 +    "photoInfo" => "C_PhotoInfo"
 +);
 +
 +?>
 +</code>
 +
 +photoinfo.php
 +<code>
 +<?php
 +
 +/**
 +  * The C_PhotoInfo class contains details for a single photo.
 +  */
 +class C_PhotoInfo {
 + /** @var int */
 + public $id;
 +
 + /** @var string */
 + public $title;
 +}
 +
 +?>
 +</code>
 +
 +photoserver.php
 +<code>
 +<?php
 +
 +/**
 +  * Manages remote requests for Photo Organizer
 +  *
 +  * This is the server for our Photo Organizer WebService.
 +  */
 +class C_PhotoOrganizerService {
 +
 +    /**
 +     * Returns the list of photos in the specified album.
 +     *
 +     * @return photoInfoList[]
 +     */
 +    public function getAlbum($id) {
 +        $l_PhotoInfoList = array();
 +
 +        // Let's make up some test data:
 +
 +        $l_PhotoInfo = new C_PhotoInfo();
 +
 +        $l_PhotoInfo->id = 42;
 +        $l_PhotoInfo->title = 'Photo Title 1 in album ['.$id.'].';
 +
 +        $l_PhotoInfoList[0] = $l_PhotoInfo;
 +
 +        $l_PhotoInfo = new C_PhotoInfo();
 +
 +        $l_PhotoInfo->id = 1024;
 +        $l_PhotoInfo->title = 'Photo Title 2 in album ['.$id.'].';
 +
 +        $l_PhotoInfoList[1] = $l_PhotoInfo;
 +
 +        return $l_PhotoInfoList;
 +     // return $l_PhotoInfo;
 +    }
 +
 +}
 +
 +?>
 +</code>
 +
 +server.php
 +<code>
 +<?php
 +
 +include_once "photoinfo.php";
 +include_once "photoserver.php";
 +include_once "common.php";
 +
 +    ini_set('soap.wsdl_cache_enabled', '0');
 +    $s = new SoapServer(
 +        'photoorganizer.wsdl',
 +        array(
 +            'trace' => 1,
 +            'classmap' => $photoClassMap
 +        )
 +    );
 +    $s->setClass('C_PhotoOrganizerService');
 +    $s->handle();
 +?>
 +</code>
 +
 +client.php
 +<code>
 +<html>
 +<head>
 +</head>
 +<body>
 +<pre>
 +<?php
 +
 +include_once "photoinfo.php";
 +include_once "common.php";
 +
 +class client {
 +
 +    private $link;
 +
 +    public function __construct($wsdl)
 +    {
 +        global $photoClassMap;
 +
 +        $this->link = new SoapClient(
 +            $wsdl,
 +            array(
 +                'trace' => 1,
 +                'classmap' => $photoClassMap
 +            )
 +        );
 +    }
 +
 +    public function getAlbum($id)
 +    {
 +        // $l_Album = array();
 +
 +        try {
 +            $l_Album = $this->link->getAlbum($id);
 +
 +            echo "var_dump:\n=========\n";
 +            var_dump ($l_Album);
 +
 +            echo "Iterate array\n=============\n";
 +
 +            // If only one element is returned, an array won't be built
 +            if (is_array($l_Album))
 +            {
 +                echo "It's an array!";
 +
 +                $i = 1;
 +                foreach ($l_Album as $photo)
 +                {
 +                    echo "\n\nPrinting element $i:\n-------------------\n";
 +                    print_r ($photo);
 +                    $i++;
 +                }
 +                echo "\n";
 +
 +            } else {
 +                print_r ($l_Album);
 +            }
 +        }
 +        catch (SoapFault $sf)
 +        {
 +            print_r ($sf->getMessage());
 +            $this->debug();
 +        }
 +            $this->debug();
 +    }
 +
 +    public function debug()
 +    {
 +        echo "Types:\n";
 +        print_r ($this->link->__getTypes());
 +        echo "Request:\n";
 +        echo htmlspecialchars(str_replace('>', ">\n", $this->link->__getLastRequest())), "\n";
 +        echo "Response:\n";
 +        echo htmlspecialchars(str_replace('>', ">\n", $this->link->__getLastResponse())), "\n";
 +    }
 +
 +}
 +
 +ini_set('soap.wsdl_cache_enabled', '0');
 +
 +echo "Running client...\n\n";
 +
 +echo "Calling server...\n";
 +
 +$client = new client($wsdlfile);
 +$client->getAlbum(27);
 +
 +echo "\nDone.\n";
 +
 +?>
 +</pre>
 +</body>
 +</html>
 +</code>
 +
podoc/webservice.1176473987.txt.gz · Last modified: 2007/04/13 14:19 by Luud