Web Services
  Home arrow Web Services arrow Page 4 - Cooking With Web Services: PHP & GD
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WEB SERVICES

Cooking With Web Services: PHP & GD
By: Ahm Asaduzzaman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 30
    2003-12-18

    Table of Contents:
  • Cooking With Web Services: PHP & GD
  • Web Services
  • PHP, GD, and NuSOAP
  • Example Walkthrough
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Cooking With Web Services: PHP & GD - Example Walkthrough


    (Page 4 of 5 )

    Map of USA

    The Problem:

    We have to develop a web application, which shows its visitors the current temperature of capital cities of each state from the West coast to the East coast of the continental United States.  Let’s, for simplicity sake, draw an imaginary line through each capital of the following states: California (CA), Nevada (NV), Utah (UT), Colorado (CO), Nebraska (NE), Iowa (IA), Illinois (IL), Indiana (IN), Ohio (OH), Pennsylvania (PA), New Jersey (NJ) and New York (NY), and also pick any zip code from the capital city of each state.

    So the visitor will see the following information:

    Temperature Plot

    Step 1:

    Find service according to your need:

    Let’s find some Web Services, which provide current temperature by region; in our case by zip code.  I chose a service from www.xmethods.net and have planned to use service names “Weather-Temperature”, which is an RPC style service and implemented with Apache SOAP; and the service developed using Java.


    Note: Remote Procedure Call (RPC)-Style Web Services are synchronous because the clients invoke the Web Service by sending parameters and waits for the method to return values before continuing. RPC-style Web Services are tightly coupled because the sending parameters and return values are as described in WSDL (Web Service Description Language) file, an XML format for describing Web Services. In RPC-style Web Services, the calling parameters and the return values are wrapped inside the SOAP Body.

    Step 2:

    The service endpoint is http://services.xmethods.net:80/soap/servlet/rpcrouter.  Let’s analyze the WSDL (Web Service Description Language) file:

    WSDL Output

    Step 3:

    Invoke the Web Service:

    <?php

    // include the NuSOAP classes
    require ('nusoap.php');
    $zipW2E='94203:89702:84102:80203:68501:50301:62701:46201:

    43085:17101:08601:12201';

    $arrZip=split(":",$zipW2E);

    //Create SOAP client
    $s=new soapclient('http://www.xmethods.net/sd/2001/TemperatureService.wsdl'
                   
    ,'wsdl');

    for ($i=0;$i < count($arrZip);$i++)
    {
    $v=$arrZip[$i];
    $parameters=array($v);
    //Make the RPC call to get temperature for each zip code selected in the array
    $graphValues[$i]=$s->call('getTemp',$parameters);
    }
    // array of states we want to show
    $States=array('CA','NV','UT','CO','NE','IA','IL','IN','OH','PA','NJ','NY');

    ?>

    Step 4:

    Plotting data using PHP GD.

    <?php
    // Define JPEG image
    header('Content-type: image/jpeg');
    // Define image size and width
    $imgWidth=350;
    $imgHeight=350;
    // Create image and define colors
    $image=imageCreate($imgWidth, $imgHeight);
    $colorWhite=imageColorAllocate($image, 0xFF, 0xFF, 0xFF);
    $colorGrey=imageColorAllocate($image, 0xEE, 0xEE, 0xEE);
    $colorDarkBlue=imageColorAllocate($image, 0xFF, 0xFF, 0xFF);
    $colorLightBlue=imageColorAllocate($image, 0x00, 0x00, 0xFF);
    $black = ImageColorAllocate($image,0,0,0);
    $red = ImageColorAllocate($image,0xFF,0,0);

    // Create border around image
    imageLine($image, 0, 0, 0, 300, $colorGrey);
    imageLine($image, 0, 0, 300, 0, $colorGrey);
    imageLine($image, 299, 0, 299, 299, $colorGrey);
    imageLine($image, 0, 299, 299, 299, $colorGrey);

    // Draw the grid for better look
    for ($i=1; $i <= 12; $i++){
    imageLine($image, $i*25, 0, $i*25, 305, $colorGrey);
    imageLine($image, 0, $i*25, 305, $i*25, $colorGrey);
    }
    //show line graph
    for ($i=0; $i<12; $i++) {
    //uncomment follwing line to see the line graph
    //imageLine($image, $i*25, (299-$graphValues[$i]*3), ($i+1)*25, (300-$graphValues[$i+1]*3), $red);
    }
    // Create bar charts and scale it
    for ($i=0; $i<12; $i++){
    $gV=$graphValues[$i]*3;
    if ($gV > 300) {
     $gv=300;
    }
    //comment following 2 lines to see only line chart
    //imageFilledRectangle($image, $i*25, (300-$gV), ($i+1)*25, 300, $colorDarkBlue);
    //imageFilledRectangle($image, ($i*25)+1, (300-$gV)+1, (($i+1)*25)-5, 298, $colorLightBlue);
    }
    // Output graph and clear image from memory
    //
    $x1=0;
    $x2=0;
    for ($i=0;$i<count($States);$i++){
    ImageString($image,2,$x1,305,''.round($graphValues[$i]).'',$black);
    ImageString($image,2,$x2,320,''.$States[$i],$black);
    $x1=$x1+25;
    $x2=$x2+25;
    }
    //output image
    imageJPEG($image);
    //cleanup
    imageDestroy($image);

    Step 5:

    Put it all together:

    <?php
    require ('nusoap.php');
    $zipW2E='94203:89702:84102:80203:68501:50301:62701:46201:

    43085:17101:08601:12201';
    $arrZip=split(":",$zipW2E);
    $s=new soapclient('http://www.xmethods.net/sd/2001/TemperatureService.wsdl','wsdl');

    for ($i=0;$i < count($arrZip);$i++)
    {
    $v=$arrZip[$i];
    $parameters=array($v);
    $graphValues[$i]=$s->call('getTemp',$parameters);
    }
    $States=array('CA','NV','UT','CO','NE','IA','IL','IN','OH','PA','NJ','NY');
    header('Content-type: image/jpeg');
    $imgWidth=350;
    $imgHeight=350;
    $image=imageCreate($imgWidth, $imgHeight);
    $colorWhite=imageColorAllocate($image, 0xFF, 0xFF, 0xFF);
    $colorGrey=imageColorAllocate($image, 0xEE, 0xEE, 0xEE);
    $colorDarkBlue=imageColorAllocate($image, 0xFF, 0xFF, 0xFF);
    $colorLightBlue=imageColorAllocate($image, 0x00, 0x00, 0xFF);
    $black = ImageColorAllocate($image,0,0,0);
    $red = ImageColorAllocate($image,0xFF,0,0);
    imageLine($image, 0, 0, 0, 300, $colorGrey);
    imageLine($image, 0, 0, 300, 0, $colorGrey);
    imageLine($image, 299, 0, 299, 299, $colorGrey);
    imageLine($image, 0, 299, 299, 299, $colorGrey);
    for ($i=1; $i <= 12; $i++){
    imageLine($image, $i*25, 0, $i*25, 305, $colorGrey);
    imageLine($image, 0, $i*25, 305, $i*25, $colorGrey);
    }
    for ($i=0; $i<12; $i++) {
    imageLine($image, $i*25, (299-$graphValues[$i]*3), ($i+1)*25, (300-$graphValues[$i+1]*3), $red);
    }
    for ($i=0; $i<12; $i++){
    $gV=$graphValues[$i]*3;
    if ($gV > 300) {
     $gv=300;
    }
    imageFilledRectangle($image, $i*25, (300-$gV), ($i+1)*25, 300, $colorDarkBlue);
    imageFilledRectangle($image, ($i*25)+1, (300-$gV)+1, (($i+1)*25)-5, 298, $colorLightBlue);
    }
    $x1=0;
    $x2=0;
    for ($i=0;$i<count($States);$i++){
    ImageString($image,2,$x1,305,''.round($graphValues[$i]).'',$black);
    ImageString($image,2,$x2,320,''.$States[$i],$black);
    $x1=$x1+25;
    $x2=$x2+25;
    }
    imageJPEG($image);
    imageDestroy($image);
    ?>

    More Web Services Articles
    More By Ahm Asaduzzaman


     

    WEB SERVICES ARTICLES

    - Safety, Idempotence, and the Resource-Orient...
    - The Resource-Oriented Architecture in Action
    - Features of the Resource-Oriented Architectu...
    - The Resource-Oriented Architecture
    - Getting Started with Flex
    - Automated Billing and Faxing for the Web
    - An Introduction to Web Services
    - The Foundations of Web Services: From Novice...
    - Web Services Reengineering: Finishing Touches
    - Fault Handling with Web Services
    - Flow and Web Services
    - Process Lifecycles and Web Services
    - Business Processes and Web Services
    - Orchestrating Web Services
    - Notifications and Resources in the WS-Resour...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek