>  기사  >  백엔드 개발  >  PHP로 웹 서비스를 개발하기 위한 샘플 코드에 대한 자세한 소개

PHP로 웹 서비스를 개발하기 위한 샘플 코드에 대한 자세한 소개

黄舟
黄舟원래의
2017-03-13 16:36:553150검색

PHP 개발 웹서비스

WSO2 WSF/PHP(WSO2 웹 서비스 프레임워크/PHP, WSO2 웹 서비스 프레임워크)는 웹 서비스를 사용하세요. SOAP1.1, SOAP1.2, MTOM, 웹 서비스 주소 지정, 웹 서비스 보안을 지원하며 REST 스타일 호출도 지원합니다. WSO2 WSF/PHP의 최신 버전(v2.0.0)이 방금 출시되었습니다.
다음은 WSO2 WSF/PHP 확장을 사용하여 간단한 계산기 서비스를 만드는 방법을 설명하는 간단한 가이드입니다.
(가정: Apache HTTP 서버가 컴퓨터에 설치되어 있고 기본적으로 Apache 서버에서 PHP 스크립트를 실행하는 데 익숙합니다.)

1단계: Ubuntu에 WSO2 WSF/PHP 확장 설치
아래 , 다음 단계가 있습니다:

1. apt-get install php5
2. apt-get install php5-dev
3. apt-get libapache2-mod-php5
3. apt-get install lib
xml
2
4. apt-get install libxml2-dev
5. 下载 WSF/PHP v2.0.0 并解压到一个目录
6. 在命令行访问该目录,以“root”执行下列命令:
./configure
make
make install
7.  /etc/init.d/apache2 restart

2단계: 계산기 서비스 작성
CalculatorService.php라는 스크립트를 생성하고 이를 Apache HTTP 서버의 웹 루트(일반적으로 / var)에 넣습니다. /www).

<?php
function calculate($inMessage){
$simplexml = new SimpleXMLElement($inMessage->str);
$operand1  = $simplexml->param1[0];
$operand2  = $simplexml->param2[0];
$operation = $simplexml->param3[0];
if($operation != null)
{
    switch($operation)
    {
         case "add" : $result= $operand1 + $operand2; break;
         case "sub" : $result= $operand1 - $operand2; break;
         case "mul" : $result= $operand1 * $operand2; break;
         case "p" : $result= $operand1 / $operand2; break;
    }
}
$response = <<<XML
        <result>$result</result>
XML;
$returnMsg = new WSMessage($response);
return $returnMsg;
}
$service = new WSService(array("operations" => array("calculate")));
$service->reply();
?>

배포되면 http://localhost:298c9bd6ad6e8c821dc63aa0473d6209/CalculatorService.php에서 액세스할 수 있습니다.

3단계: 계산기 클라이언트 작성
이 계산기 서비스를 호출하고 결과를 인쇄하는 클라이언트를 작성합니다.
스크립트 이름은 CalculatorClient.php이며 Apache HTTP 서버의 웹 루트에 배치됩니다.
Apache 서버의 포트(예: http://www.php.cn/:81/CalculatorService.php)를 서버와 일치하도록 변경하는 것을 잊지 마세요.

<?php
$requestPayload = <<<XML
<calculate>
<param1>100</param1>
<param2>43</param2>
<param3>add</param3>
</calculate>
XML;
try{
$message = new WSMessage($requestPayload,
            array("to" => "http://localhost:81/CalculatorService.php"));
$client = new WSClient();
$response = $client->request($message);    
echo "Answer : $response->str";
}
catch (Exception $e){   
if ($e instanceof WSFault){
  $fault = $e;
  printf("Soap Fault received. Code: &#39;%s&#39; .Reason: &#39;%s&#39;/n",
                  $fault->code, $fault->reason);
}else{
  printf("Exception occurred. Message: &#39;%s&#39;/n", $e->getMessage());
}
}
?>

4단계: 서비스에 액세스
다음과 같이 CalculatorClient.php를 실행하여 서비스에 액세스합니다.
http://localhost:< ;포트> ;/CalculatorService.php

위 내용은 PHP로 웹 서비스를 개발하기 위한 샘플 코드에 대한 자세한 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.