Webservice, API call from php code
Webservice | Api in php development.
Deal with webservcie in php, consume any API in php code.
Here I explain how you can make any API/WebService call from php, In simple language you need to know the structure of generated xml fom API/Webservice,
making call is simply sending a request to the server with right parameter.
Here look at the result of API call, which give a report in an XML format when we call a function called GetReport(AuthKey,ReportID), first we look how the result look like.
 | Result of GetReport(AuthKey,ReportID) |
| <xml>
| | 1: <report_details>
| | 2: <report_name><![CDATA[This is a sample Report]]></report_name>
| | 3: <report_id><![CDATA[1036067983]]></report_id>
| | 4: <submit_date><![CDATA[2009-10-08 21:36:34]]></submit_date>
| | 5: <job_status><![CDATA[Completed]]></job_status>
| | 6: <preview_clients>
| | 7: <client>
| | 8: <client_code>CLIENT001</client_code>
| | 9: <client_name><![CDATA[Tata Consultancy]]></client_name>
| | 10: <client_description><![CDATA[TCS is a great company]]></client_description>
| | 11: </client>
| | 12: <client>
| | 13: <client_code>CLIENT002</client_code>
| | 14: <client_name><![CDATA[Infosys]]></client_name>
| | 15: <client_description><![CDATA[Infosys is a big name in IT world]]></client_description>
| | 16: </client>
| | 17: </preview_clients>
| | 18: </report_details>
| | 19:</xml>
| | 20: |
|
Now we see how to make GetReport(AuthKey,ReportID) call to the API
 | How to make the call from php code. |
|
| | 1:$request = "http://thirdpaty.com/api/ command=GetReport&AuthKey=myauthKey&ReportID=225";
| | 2:
| | 3:or if you want to receive the value from form submit.
| | 4:$request = "http://thirdpaty.com/api/ command=GetReport&AuthKey=myauthKey&ReportID=".$_POST["ReportID"];
| | 5:
| | 6:// Now lets send a request to the server, and get the result into SimpleXML object
| | 7:$sxml = simplexml_load_file($request);
| | 8:
| | 9:Now to play with this object we need to know the format of the
| | 10: |
|
If we look at the above xml result that we got in retun of GetReport call, the xml structure is bit complex, but in php you can ead the structure very easily.
 | Read XML result in php |
| <?php
| | 1:
| | 2:$request = "http://thirdpaty.com/api/ command=GetReport&AuthKey=myauthKey&ReportID=".$_GET["ReportID"];
| | 3:// read feed into SimpleXML object
| | 4:$sxml = simplexml_load_file($request);
| | 5:
| | 6:
| | 7:echo "<div style='margin-top:12px;'><table cellspacing='1' cellpadding='1' width='100%'>";
| | 8:echo "<tr style='font-weight:bold;background-color:#abf9b6;'><td colspan='3'>". $sxml->report_details->report_name ."</td></tr>";
| | 9:
| | 10:echo "<tr style='background-color:#dffee3;height:26px;'>";
| | 11:foreach ($sxml->report_details->preview_clients->client as $entry)
| | 12: {
| | 13:
| | 14: echo "<td style='background-color:#dffee3;'>". $entry->client_code ."<br><img src='". $entry->client_name ."' style='width:100px;height:100px;'></td>";
| | 15:
| | 16: echo "<td style='background-color:#dffee3;'>". $entry->client_description ."</td>";
| | 17:
| | 18:
| | 19: }
| | 20:echo "</tr>";
| | 21:echo "</table></div>";
| | 22:?>
| | 23: |
|
Now we see how to create a webservice in php
Asp.net, Ado.net, .Net Remoting, .Net
Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML