亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 PHP5中使用Web服務(wù)訪問J2EE應(yīng)用程序(4)

PHP5中使用Web服務(wù)訪問J2EE應(yīng)用程序(4)

Jun 21, 2016 am 09:15 AM
http php soap web

j2ee|php5|web|web服務(wù)|程序|訪問

處理 SOAP 錯(cuò)誤

  如果運(yùn)行客戶機(jī)時(shí)出現(xiàn)錯(cuò)誤怎么辦?與其他語言(如 Java)一樣,PHP 5 新增加了一種異常機(jī)制。ext/soap 使用這種新的機(jī)制,以 SoapFault 對象的形式返回錯(cuò)誤。比方說,可以用下面這種形式將代碼包裝起來:

try {
... some SOAP operation
} catch (SoapFault $soapFault) {
echo $soapFault;
}
  注意,與 Java 有所不同,PHP 語言的 try - catch 塊不能包含 finally 子句。

  SoapFault 可以在本地生成。比方說,假設(shè)輸錯(cuò)了 getForecast 的 startDate 參數(shù)??蛻魴C(jī)的輸出就會(huì)變成:

SoapFault exception: [SOAP-ENV:Client]
SOAP-ERROR: Encoding: object hasn't 'startDate' property in WeatherClientEJB.php:32
Stack trace: #0 WeatherClientEJB.php(32): SoapClient->getForecast('getForecast', Array)
#1 WeatherClientEJB.php(73): displayForecast(Array)
#2 {main}
  注意,其中沒有 trace 輸出,因?yàn)椴]有發(fā)送請求。SOAP_ENV:Client 是 SOAP 規(guī)范中為 Faulty body 元素 Faultcode 字段定義的值之一。

  這個(gè) SoapFault 是在 ext/soap 內(nèi)部發(fā)現(xiàn)錯(cuò)誤時(shí)生成的,它沒有發(fā)送 SOAP 消息。但是 SoapFaults 也可以報(bào)告服務(wù)器上發(fā)現(xiàn)的錯(cuò)誤。比如,假設(shè)修改代碼,將 startDate 參數(shù)的值設(shè)成“badDateString”。這是一個(gè)非法的 ISO 8601 字符串,但是 ext/soap 沒有檢查提供的格式,僅僅把消息發(fā)送到服務(wù)器,而服務(wù)器拒絕該請求:

Request :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://session.itso">
<SOAP-ENV:Body>
<ns1:getForecast>
<ns1:startDate>badDateString</ns1:startDate>
<ns1:days>2</ns1:days>
</ns1:getForecast>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response :
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<Fault xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode xmlns="">Server.generalException</faultcode>
<faultstring xmlns="">
<![CDATA[java.lang.NumberFormatException:
WSWS3046E: Error: Invalid date/time: badDateString]]>
</faultstring>
<detail xmlns=""/>
</Fault>
</soapenv:Body>
</soapenv:Envelope>

SoapFault exception: [Server.generalException] java.lang.NumberFormatException:
WSWS3046E: Error: Invalid date/time: badDateString in WeatherClientEJB.php:32
Stack trace: #0 WeatherClientEJB.php(32): SoapClient->getForecast('getForecast', Array)
#1 WeatherClientEJB.php(73): displayForecast(Array)
#2 {main}
  這一次,SOAP 請求傳遞給了服務(wù)器,但是因?yàn)槿掌诟袷綗o效而被拒絕。WeatherForecastEJB 實(shí)現(xiàn)拋出一個(gè) java.lang.NumberFormatException,該異常在 SOAP 應(yīng)答中作為 Faulty body 元素返回,然后作為一個(gè) SoapFault 異常報(bào)告給客戶機(jī)。

  保護(hù) Web 服務(wù)

  我們考察了三種安全方法,以及如何在 PHP 中使用它們:

  基本 HTTP 身份驗(yàn)證

  如果 HTTP 服務(wù)器要求客戶機(jī)進(jìn)行身份驗(yàn)證,就會(huì)請求用戶輸入 id 和口令并在應(yīng)答中增加 Authentication Required HTTP 頭文件。在進(jìn)行后續(xù)操作之前,客戶機(jī)必須響應(yīng)包含可接受 Authorization HTTP 頭文件的請求。

  請求 HTTP 身份驗(yàn)證的通常是 Web 服務(wù)器,而不是 Web 服務(wù)提供者。Authentication Required HTTP 頭文件被傳遞給瀏覽器,瀏覽器彈出對話框請求用戶 id 和口令,然后將用戶的應(yīng)答作為 HTTP Authorization 頭文件發(fā)送給 Web 服務(wù)器。在 PHP 腳本中很容易實(shí)現(xiàn)這一點(diǎn),可以使用 header() 函數(shù)發(fā)送需要的 HTTP 頭文件字段。例如:

if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Weather"');
header("HTTP/1.0 401 Unauthorized");
}
echo "Welcome " . $_SERVER['PHP_AUTH_USER'];
  PHP 手冊中的使用 PHP 進(jìn)行 HTTP 身份驗(yàn)證 一章詳細(xì)介紹了這個(gè)過程。

  您可能遇到這樣一些 Web 服務(wù),這些服務(wù)的提供者要求 PHP Web 服務(wù)客戶機(jī)使用 HTTP 進(jìn)行驗(yàn)證身份。ext/soap 提供了一種簡單的發(fā)送 HTTP Authorization 請求頭文件的方法,使用傳遞給 SoapClient 構(gòu)造函數(shù)的 options 數(shù)組:

$soapClient = new SoapClient("http://localhost:9080/" .
"ItsoWebService2RouterWeb/wsdl/itso/session/WeatherForecastEJB.wsdl",
array('login' => "userid",
'password' => "password"));
  但是,人們認(rèn)為 HTTP 基本身份驗(yàn)證不是一種安全的用戶驗(yàn)證方法(除非結(jié)合使用其他外部安全系統(tǒng),如 SSL),因?yàn)橛脩裘涂诹钍且悦魑男问皆诰W(wǎng)絡(luò)上傳遞的。 HTTP Digest 驗(yàn)證通過加密口令改進(jìn)了這種方法,但是并不是所有的瀏覽器都支持這種改進(jìn)。而且,PHP 的 header() 函數(shù)只支持基本身份驗(yàn)證。

  SSL(安全套接字層)

  一種更加安全的協(xié)議是 HTTPS(HTTP over SSL),它使用 SSL 加密 HTTP 消息。SSL 在傳輸層上工作,不了解 HTTP 或 SOAP 協(xié)議。因此,它不能只加密消息中的敏感成分,而必須加密整個(gè)消息。HTTPS 可以在瀏覽器與 Web 服務(wù)器之間,或者 Web 服務(wù)器與 Web 服務(wù)提供者之間使用。

  如果編譯并啟用了 OpenSSL,那么 PHP 還可以支持 HTTPS。如何在 PHP 腳本中使用 SSL,請參閱 PHP 手冊中的 OpenSSL 一章。

  身份驗(yàn)證怎么樣呢?SSL 可以發(fā)送安全證書,對方可以接受或拒絕該安全證書。如果要求客戶機(jī)驗(yàn)證 Web 服務(wù)提供者(如電子商務(wù)應(yīng)用程序),那么這種方法很有效。但是如果 Web 服務(wù)本身提供敏感信息的訪問,那么 Web 服務(wù)提供者還是需要驗(yàn)證每個(gè)客戶?;谧C書的身份驗(yàn)證并不合適,因?yàn)榭蛻艨赡芎芏?,而且是?dòng)態(tài)的,事先為每個(gè)客戶分發(fā)適當(dāng)?shù)淖C書不太現(xiàn)實(shí)。

  WS-Security

  WS-Security 標(biāo)準(zhǔn)為 Web 服務(wù)安全提供了不同的方法。目前我們所考察的安全控制都在 SOAP 協(xié)議之外。但是 WS-Security 是通過在 SOAP 消息中增加安全頭文件來實(shí)現(xiàn)安全控制的。比如,對于 WS-Security 基本身份驗(yàn)證(與 HTTP 基本身份驗(yàn)證不同),下面的標(biāo)簽將出現(xiàn)在 SOAP 頭文件中:

<wsse:UsernameToken>
<wsse:Username>userid</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
  這只是一個(gè)簡單的例子,不過,完整的安全擴(kuò)展集是非常完善的,不僅包括身份驗(yàn)證,還包括完整性、保密性,等等。

  目前 ext/soap 中還沒有為 WS-Security 提供很好的支持。因此,如果要在 PHP 中發(fā)送和接收 WS-Security 頭文件,那么必須深入到更底層的接口,顯式地創(chuàng)建 SOAP 頭文件。到目前為止,示例中使用的都是 ext/soap 的 WSDL 模式。但是,還有一種非 WSDL 模式,可以用它來控制整個(gè) SOAP 消息。當(dāng)然也必須在代碼中做大量的工作??梢允褂?SoapHeader、SoapParam 和 SoapVar 類創(chuàng)建消息,然后用 SoapClient::__call 發(fā)送 SOAP 請求和接收響應(yīng)。如果沒有任何內(nèi)置支持,那么在 PHP 中編寫 Web 服務(wù)安全擴(kuò)展(或者其他高級(jí)規(guī)范如 WS-Transactions)將是一項(xiàng)相當(dāng)艱巨的任務(wù),我們不打算在本文中進(jìn)行這方面的嘗試。

  結(jié)束語

  使用 PHP SOAP 擴(kuò)展并不難。無論服務(wù)器是如何實(shí)現(xiàn)的,只需要幾行代碼,就可以開發(fā)出訪問簡單 Web 服務(wù)的 PHP 腳本。與往常一樣,PHP 在易用性上表現(xiàn)非常出色。本文主要討論了如何使用 SoapClient 類訪問異構(gòu)網(wǎng)絡(luò)上的現(xiàn)有 Web 服務(wù),但是通過使用 ext/soap,利用 SoapServer 類也可以部署 Web 服務(wù),同樣非常直觀。

  如果要處理更復(fù)雜的交互,ext/soap 當(dāng)前的版本不能給我們提供很多幫助。從 XML 模式到 PHP 的映射有時(shí)候不夠清晰,只能通過試驗(yàn)或者研究源代碼來驗(yàn)證。如果希望使用更先進(jìn)的 Web 服務(wù)協(xié)議,惟一的選擇就是深入研究非 WSDL 模式,用自己的腳本創(chuàng)建 SOAP 頭文件,但這樣做非常乏味而且容易出錯(cuò)。

  Web 服務(wù)的一項(xiàng)重要主張是不同平臺(tái)、操作系統(tǒng)和編程語言的互操作性。獨(dú)立的 WS-I (Web Services Interoperability) Organization 提供了一個(gè)測試包,以驗(yàn)證對其 Basic Profile 的適應(yīng)性,我們希望看到 ext/soap 能夠達(dá)到某種程度,表現(xiàn)出它能夠適應(yīng)。我們也希望 ext/soap 繼續(xù)發(fā)展,成為 PHP 的主流擴(kuò)展。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization Jul 25, 2025 pm 08:57 PM

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism Jul 25, 2025 pm 08:30 PM

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution Jul 25, 2025 pm 07:06 PM

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.

See all articles