Home arrow Web Services arrow Page 4 - Cooking With Web Services: PHP & GD
WEB SERVICES

Cooking With Web Services: PHP & GD


Web ServicesAsad's latest article shows us how easy Web Services can be employed using a combination of ingredients, including PHP and GD.  Using an example Web Service, Asad shows us how to deploy our own.  So, if you're interested in learning a little bit more about this so-called "phenomenon", be sure to have a read.

Author Info:
By: Ahm Asaduzzaman
Rating: 5 stars5 stars5 stars5 stars5 stars / 33
December 18, 2003
TABLE OF CONTENTS:
  1. · Cooking With Web Services: PHP & GD
  2. · Web Services
  3. · PHP, GD, and NuSOAP
  4. · Example Walkthrough
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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);
?>


blog comments powered by Disqus
WEB SERVICES ARTICLES

- Dealing with Loose Coupling in a Service-Ori...
- Loose Coupling in a Service-Oriented Archite...
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials