Asad'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.
Cooking With Web Services: PHP & GD - Example Walkthrough (Page 4 of 5 )
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:
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.
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'); … ?>
// 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);