目錄
class soapserver not found
首頁 php教程 php手册 PHP Webservice的发布与调用

PHP Webservice的发布与调用

Jun 06, 2016 pm 07:55 PM
php webservice 發布 呼叫

PHP Webservice的发布与调用 1. 环境配置 配置php.ini,把php_soap.dll前面的分号去掉, 不然会报错 class soapserver not found 重启apache后通过phpinfo()查看 这样是表示环境已经支持soap的webservice了,后面的事情就是写代码了。 2. webservice的发布

 

PHP Webservice的发布与调用

 

1.  环境配置

配置php.ini,把php_soap.dll前面的分号去掉,

 

不然会报错

class soapserver not found

 

 PHP Webservice的发布与调用

重启apache后通过phpinfo()查看

这样是表示环境已经支持soap的webservice了,后面的事情就是写代码了。

 PHP Webservice的发布与调用

 

2.  webservice的发布

发布出来的*.wsdl文件,其实是一个xml格式的文件,生成这个文件可以通过第3方软件,如ZendStudio 就可以生成。

 

示例,我用以下代码生成:

<?php class CTest

{

       public function __construct()

       {

              

       }

       

       /**

        *

        * @param string $oParams

        * @return string

        */

       public function Add($oParams)

       {

              $sParams = $oParams->oParams[l1] ;

              $oParams = json_decode($sParams);

              $a = $oParams->a;

              $b = $oParams->b;

              $c = $a+$b;

              return array('AddResult'=>$c);

       }

}

?>

登入後複製


 

生成的xml文件是:

<?xml version="1.0" encoding="UTF-8" ?> 

 <definitions targetnamespace="http://tempuri.org/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

 <types>

 <schema elementformdefault="qualified" targetnamespace="http://tempuri.org/">

 <element name="AddRequest">

 <complextype>

 <sequence>

  <element minoccurs="0" maxoccurs="1" name="oParams" type="s:string"></element> 

  </sequence>

  </complextype>

  </element>

 <element name="AddResponse">

 <complextype>

 <sequence>

  <element minoccurs="0" maxoccurs="1" name="AddResult" type="s:string"></element> 

  </sequence>

  </complextype>

  </element>

  </schema>

  </types>

 <message name="AddSoapIn">

  <part name="parameters" element="tns:AddRequest"></part> 

  </message>

 <message name="AddSoapOut">

  <part name="parameters" element="tns:AddResponse"></part> 

  </message>

 <porttype name="CTestSoap">

 <operation name="Add">

  <input message="tns:AddSoapIn"> 

  <output message="tns:AddSoapOut"></output> 

  </operation>

  </porttype>

 <binding name="CTestSoap" type="tns:CTestSoap">

  <binding transport="http://schemas.xmlsoap.org/soap/http"></binding> 

 <operation name="Add">

  <operation soapaction="http://tempuri.org/Add" style="document"></operation> 

 <input>

 <output>

  <body use="literal"></body> 

  </output>

  </operation>

  </binding>

 <binding name="CTestSoap12" type="tns:CTestSoap">

  <binding transport="http://schemas.xmlsoap.org/soap/http"></binding> 

 <operation name="Add">

  <operation soapaction="http://tempuri.org/Add" style="document"></operation> 

 <input>

 <output>

  <body use="literal"></body> 

  </output>

  </operation>

  </binding>

 <service name="CTest">

 <port name="CTestSoap" binding="tns:CTestSoap">

  <address location="http://192.168.6.44:80/webservice/server/server.php?wsdl[l2] "></address> 

  </port>

 <port name="CTestSoap12" binding="tns:CTestSoap12">

  <address location="http://192.168.6.44:80/ webservice/server/server.php?wsdl[l3] "></address> 

  </port>

  </service>

  </definitions>

登入後複製


 

 

生成xml文件后,就要通过一个服务发布出去,如:

<?php error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); 

include_once('../class/test.class.php');[l4] 

$server = new SoapServer('wscache/CTest.1.0.wsdl');

$server->setClass('CTest');

$server->handle();

?>

登入後複製


 

然后可以向其他人或者系统提供出发布的地址,如:

http://192.168.6.44/webservice/server/server.php?wsdl

 

3.  webservice的调用

通过上面提供的地址,写一个调用页面

<?php error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); 

header("Content-Type: text/html;charset=utf-8");

$client = new SoapClient('http://192.168.6.44/webservice/server/server.php?wsdl');

$str = '{"a":1,"b":20}';

//调用方法一

$r = $client->Add(array('oParams'=>$str)); //数组

//调用方法二

//$pParams->oParams = $str;

//$r = $client->__call('Add',array($pParams));//这个得是对象

var_dump($r);

?>

登入後複製

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

在本章中,我們將了解CakePHP中的環境變數、常規配置、資料庫配置和電子郵件配置。

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

為了在 cakephp4 中處理日期和時間,我們將使用可用的 FrozenTime 類別。

CakePHP 使用資料庫 CakePHP 使用資料庫 Sep 10, 2024 pm 05:25 PM

在 CakePHP 中使用資料庫非常容易。本章我們將了解CRUD(建立、讀取、更新、刪除)操作。

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

為了進行文件上傳,我們將使用表單助理。這是文件上傳的範例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我們將學習以下與路由相關的主題?

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的開源框架。它旨在使應用程式的開發、部署和維護變得更加容易。 CakePHP 基於類似 MVC 的架構,功能強大且易於掌握。模型、視圖和控制器 gu

CakePHP 建立驗證器 CakePHP 建立驗證器 Sep 10, 2024 pm 05:26 PM

可以透過在控制器中新增以下兩行來建立驗證器。

See all articles