Upload
trinhtuong
View
225
Download
0
Embed Size (px)
Citation preview
PHP Web Services with NuSOAP 1/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
:: PHP Web Services with NuSOAP ::
���������� ������
1. ������ NuSOAP 2. ������� ������������ ���� 3. �� � ���������� ����������������� PHP + NuSOAP 4. �� � ���������������� �������� PHP + NuSOAP 1. ������ NuSOAP
NuSOAP � !��" #$��%#�&�$'������(�)*��������%� +� ���������� �����,�� ���������������� ����-����������� .�� NuSOAP '� �� �%",�����/ %��%��-����-0,� nusoap.php �����-0,�������&����� NuSOAP '��(�)*��������%� +� ���������� ���� .��)*��������%� +��� NuSOAP � ����,�� ���������� ����&�$� !� Simple Type �,� Complex Type -��������%�* 1� �� 2''3��� ��� ���$�,���3�"#� 1.95 ����&�$'�� �$%� ���������� ����)*������'����(�-0,� nusoap.php %������(�-������ �6$�-0,�����%� +�����.(,�-��'�� http://cvs.sourceforge.net/viewcvs.py/nusoap/lib/nusoap.php �%#$�-��-0,�%��,���(�&���� "��,��- -�����-�� ��&� �$&�$'�&���� �����. �� % )*������'����%� Web Server &�$������ PHP -�� �6$���%� +� !�-��&����� Local host �,����%� Domain Name ' ��
PHP Web Services with NuSOAP 2/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
����� 1 ." �� ����� &�������� PHP Web Services
nusoap.php
nusoap.php Client
Server
hello.php
call_hello.php
Soap Request
Soap Response
Create Instance
Create Instance
2
3
4
1
:: Output :: Hello , Dr. Prawase
5
PHP Web Services with NuSOAP 3/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
2. ��������������� �!" #��$�%&
1. )*������� ���������� �������( ���(�� ��� (Implementation or create web services) 2. )*������&���� ����������� ���� (Deploy Web Services) 3. )*������&���� ,�&������������� ������#$�� :�� ��� (UDDI Registry) 4. )*�������&���� �#�"����#$�(�� ��� ��� / &�$����� -��'�� UDDI (Finding Web Services and Consume Web Services) ��� 1�����'����"��� ��� -�� 3. ���&���� �!" #��$�%&���������)��� PHP + NuSOAP �S������ TUV �����W�#XY� �Z� hello �"[ goodbye \�!�S������TYV ����"[]ZU�#��^�W��T!���W��_�Z�$ "Hello ,���W�!]ZU�^�W��T!�a]W�S������" �"[ "Good Bye , ���W�!]ZU�^�W��T!�a]W�S������"
��ZU���W����_V��W�V �"Y�]ZU�de��TY�$ hello.php ################### Begin of file hello.php ###################### <?php require_once('nusoap.php');
//��W�em���]"U TU�W����a�W[���� \�!em���]"UTY]ZU��$ hello function hello($name){ return "Hello, " . $name; } function goodbye($name){ return "Good Bye, " . $name; } $ws_server = new soap_server; // ��W� instance #�� Soap-Server //*** ����T!�a]Wem���]"U configureWSDL #�� Soap-Server V �a�Wd�W WSDL File �W�! $ws_server->configureWSDL('hellowsdl', ' http://campus.en.kku.ac.th/webservices/v);
PHP Web Services with NuSOAP 4/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
// ����� �[T!em���]"U TU�W����a�W[���� //function register($name,$input=array(),$output=array(),$namespace=false, //$soapaction=false,$style=false,$use=false,$documentation='',$encodingStyle=vv) $ws_server->register('hello', array('name' => 'xsd:string'), array('return' => 'xsd:string'), 'http://campus.en.kku.ac.th/webservices/v, 'http://campus.en.kku.ac.th/webservices/hello', 'rpc', 'encoded', 'Says hello to the caller') ;
///0121345/6789:;<=52>?@A;/9ABCD5213EFC83G213
$ws_server->register('goodbye', array('name' =>'xsd:string'), array('return' =>'xsd:string'), 'http://campus.en.kku.ac.th/webservices/v, 'http://campus.en.kku.ac.th/webservices/goodbyev, 'rpc', 'encoded', 'Says Good Bye to the caller'); // Use the request try to invoke the service $ws_server->service($HTTP_RAW_POST_DATA); ?> ################### End of file hello.php ######################
PHP Web Services with NuSOAP 5/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
V�"Y�_V [" X�de��TY�xy hello.php de��TYV z����_[d�W TU localhost �"����a]W� php 5.1.1 == > http://localhost/phpws/
3HI/9A 2 PATH TU��_[de��TY�_�Z� http://localhost/phpws/hello.php
*+ $ hello.php ��*)�������&��������,�
*+ $ nusoap.php ��-.�-����&���� Instance ��� Soap-Client � � Soap-Server
PHP Web Services with NuSOAP 6/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
��ZU��de�� hello.php dx�"^$ web browser �_V d�W^��"�TY
3HI/9A 3 ��ZU���T!�de�� hello.php ^$ web browser
V���x TU 2 V ��_�$��ZU�����T!� hello.php ^$ web browser V �T������em���]"U TUa�W[�����Z� hello �� good bye
,/�0 %���&)� WSDL ��� #��$�%&��&��������,�1)� NuSOAP -������ 4
,/�0 %���&)�3 )������� 5
,/�0 %���&)�3 )������� 6
PHP Web Services with NuSOAP 7/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
3HI/9A 4 ���� WSDL file #�� hello.php TU��W�\�! NuSOAP
PHP Web Services with NuSOAP 8/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
3HI/9A 5 �T�����x�$�$�} V� WSDL file �a�W��ZU�a�W�$!��$���$
PHP Web Services with NuSOAP 9/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
3HI/9A 6 �T�����x�$�$�} V� WSDL file �a�W��ZU�a�W�$!��$���$
��ZU����_VV�����W��S��������ZU�a�W[���� �$�dxV �xy����T!�a]W���_[�S������ TU��d�W��W�#XY��$��WTY \�!V �"Y�]ZU�de��TY�$ call_hello.php
PHP Web Services with NuSOAP 10/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
4. ��� ����-.���� �!" #��$�%&)��� PHP + NuSOAP ################### Start call_hello.php ###################### <? require_once('nusoap.php'); //��W� Instance #�� Soap-Client ��ZU�a�W���z��T!�a]W�em���]"Ud�W \�!���������V �xy URL #�� WSDL // newsoapclient($endpointAddr, true if $endpointAddr is WSDL file, $proxyhost,$proxyport); $ws_client = new soapclient(�http://localhost/phpws/hello.php?wsdl',true); //��T!�a]W�S������ TUd�W��W�#XY� (hello function) $result = $ws_client->call('hello', array('name' => �Dr.Prawase Wasi')); //����^� echo $result; echo �<br/>�; //��T!�a]W�S������ TUd�W��W�#XY� (goodbye function) $result = $ws_client->call('goodbye',array('name'=>'Dr. Prawase Wasi')); //����^� echo $result ; ?> ################### END call_hello.php ######################
PHP Web Services with NuSOAP 11/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
�"��!$��$�dxV �xy����T!� Amazon Web Service \�! TUde��TYV ��T!�a]W nusoap.php
##################### Begin of call_amazonws.php ############################
<?php
require_once('nusoap\nusoap.php');
// for calling web service through proxy that is set up by kku
$proxyhost="202.12.97.116";
$proxyport="8080";
// create a instance of the SOAP client object
// remember that this script is the client,
// accessing the web service provided by Amazon.com
$client = new
soapclient("http://soap.amazon.com/schemas2/AmazonWebServices.wsdl",
true,$proxyhost,$proxyport);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error </h2><pre>'.$err.'</pre>';
}
PHP Web Services with NuSOAP 12/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
// set up an array containing input parameters to be
// passed to the remote procedure
$params = array(
'keyword' => 'web service',
'browse_node' => 20,
'page' => 1,
'mode' => 'books',
'tag' => 'webservices-20',
'type' => 'lite',
'devtag' => '16XT8ETKKB7NWHAGCQ02'
);
// invoke the method
$p = $client->getProxy();
$result = $p->KeywordSearchRequest($params);
if ($client->fault) {
echo '<h2>Fault (This is expected)</h2></pre>';
print_r($result);
echo '</pre>';
} else {
PHP Web Services with NuSOAP 13/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
$err = $client->getError();
if ($err) {
echo '<h2>Error</h2><pre>'.$err.'</pre>';
} else {
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
?>
################### End of call_amazonws.php ##############################3
SXU�V d�W^��"����"�����a��x TU 7
PHP Web Services with NuSOAP 14/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
��x TU 7 ����^��"���V�����T!�a]W Amazon Web Service \�!a]W NuSOAP
�����(;��,��������� ����&�$� ��(��%��'�� !���� RPC Style �������� � �����������'�&��-��-%����������"�)*������&���� ����+6� Operation &�$����� � ������������� ��'�%�������� ������� ��<&(�6$�&�$����%� �� 3�� #$������� &����� ��%�������3 � 1����/ �(���%� +&����� ��%���-�������-%�%� 2;(� ������� ���� ��<&��� ������ Document Style ��,�)*������� ���������'�-%���%� +���(��-������ Operation �����'�������(��)��� Soap Action ��%� �6$��� � �����������-%��3�����%����� )*������'����%�"��%�����' WSDL � !�������� )*��������� *���� Soap action ��&�$)*�����������
�������+��- '�� !��� � ������������� ����&�$� !���� Document Style ������� ������� &. �(�� ��&=-&� http://www.pttplc.com/pttinfo.asmx?WSDL
������6�%�.����� .NET ��� Microsoft �6$� Default ��� .NET '�� ���������� �����(�� !���� Document Style �(���*��,�� ��������� binding &�$��*��� WSDL '�����(�� � *����������� ����&�$������6�%���� !���� Document �����#$�%��*����������%�.�� �� ��$���- �(�� ���������� .����%� +�*-��&�$��,��%�� <portType> �6$�'�%���,��%�� <operation> ���/ ������*�<���� '������,#��.�� �� ��$�&�$���'&�$'�� ��� ��&�$�����'&�$'�� ���.�� �� ��$� �GetOilPrice� '�������'� 1����'����������%*,�����- .���*&�$ input message ��� .�� �� ��$� �GetOilPrice� �6$������'����- �* message �s0:GetOilPriceSoapIn�
PHP Web Services with NuSOAP 15/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
* &�$ 8 ��,��%�� portType �6$�����.�� �� ��$���� / &�$��%� +� ������-��
* &�$ 9 ��,��%�� message ��-0,� WSDL �6$�������#�(�&�$'����%������"��%&�$����,� �� '�� * &�$ 9 �6$��������%*,<���� message �GetOilPriceSoapIn� �����'�%���,��%�� �s0:GetOilPrice� ��*�<���� �� ��>���������- "#�- �* ��,�����������,��%�� �GetOilPrice� �6$���*�<������,��%�� <s:schema> �6$���*�<������,��%�� <types>
PHP Web Services with NuSOAP 16/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
* &�$ 10 ��,��%�� element �?����,��@1����%*,�����,��%�� �GetOilPrice� '�� * &�$ 10 � �������%����%*,�����%�� ������,��%�� �GetOilPrice� �6$���%� +& ��-��'���� �'�����,��%�� <s:schema>
* &�$ 11 ��,��%�� schema �6$�%��� ����%�,��� ���=�����,��%����� / '�� * &�$ 11 &���(�& �������%�� ������,��%�� GetOilPrice �6$���*�<������,��%�� schema ���"#� http://www.pttplc.com/ptt_webservice/ '�� * &�$ 10 �,� 11 &���(�& ��������%*,&�$'�������- ��%� +� ���.����� PHP code ����� $document = '<GetOilPrice xmlns="http://www.pttplc.com/ptt_webservice/">'; $document .= ' <Language>'.$lang.'</Language>'; $document .= '<DD>'.$day.'</DD>'; $document .= '<MM>'.$month.'</MM>'; $document .= ' <YYYY>'.$year.'</YYYY>'; $document .='</GetOilPrice>';
PHP Web Services with NuSOAP 17/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
'������ �'����& �� Soap Action ��#$��6����%*, GetOilPrice ��� & �6$���%� +& ��-��'���� �'�����,��%�� <operation> �6$���*�<������,��%�� <soap:binding> �6$���*�<������,��%�� <binding>
* &�$ 12 ��,��%�� binding �6$�%����%*, binding �,� soapAction ���.�� �� ��$���� / ######## Begin of file �pttwsGetOilPrice.php ################# <? include("nusoap.php"); $wsdl = "http://www.pttplc.com/pttinfo.asmx?WSDL"; //,��"� http://www.pttplc.com/pttinfo.asmx?op=GetOilPrice ��'���������� Soap action ��- //�,�"� '���� Document ." �� ������-(� //initial soap action $soapaction = "http://www.pttplc.com/ptt_webservice/GetOilPrice"; $lang = 'EN' ; $day = '24' ; $month = '04'; $year = '2006' ; $proxyhost = �202.12.97.116�; $proxyport = �8080�; $client = new soapclient($wsdl,true, $proxyhost, $proxyport); $document = '<GetOilPrice xmlns="http://www.pttplc.com/ptt_webservice/">';
PHP Web Services with NuSOAP 18/18
��. �� ��� ��� �� ����� ������ ������������������� ���� !�"!#���$
$document .= ' <Language>'.$lang.'</Language>'; $document .= '<DD>'.$day.'</DD>'; $document .= '<MM>'.$month.'</MM>'; $document .= ' <YYYY>'.$year.'</YYYY>'; $document .='</GetOilPrice>'; //create soap message $mysoapmsg = $client->serializeEnvelope($document,'',array(),'document', 'literal'); //send soap message to soap server $response = $client->send($mysoapmsg, $soapaction); print_r($response); ?> ######## End of file �pttwsGetOilPrice.php #################
* &�$ 13 ), �����-0,�&�$� ���������� ������� & ��#$�& �� �"����%�������&�$ 23/04/2006