告別NTLM認(rèn)證難題:jamesiarmes/php-ntlm如何助力PHP應(yīng)用連接Microsoft服務(wù)

在開發(fā)過程中,我需要使用 php 應(yīng)用與公司的 Exchange Server 進(jìn)行交互,獲取郵件信息。然而,Exchange Server 使用 NTLM 認(rèn)證,而 PHP 原生的 SoapClient 在處理 NTLM 認(rèn)證時(shí)非常麻煩,需要手動(dòng)設(shè)置 cURL 選項(xiàng),并且容易出錯(cuò)。我嘗試了多種方法,但都未能找到一個(gè)簡單易用的解決方案。

經(jīng)過一番搜索,我發(fā)現(xiàn)了 jamesiarmes/php-ntlm 庫。它專門用于處理 php 應(yīng)用與 microsoft 服務(wù)的 ntlm 認(rèn)證問題,提供了一個(gè)易于使用的 soapclient 擴(kuò)展類,可以簡化 ntlm 認(rèn)證的配置過程。

composer在線學(xué)習(xí)地址:學(xué)習(xí)地址

使用 Composer 安裝 jamesiarmes/php-ntlm 庫非常簡單:

composer require jamesiarmes/php-ntlm

安裝完成后,就可以使用 jamesiarmesPhpNtlmSoapClient 類來連接 Exchange Server 了。以下是一個(gè)簡單的示例:

use jamesiarmesPhpNtlmSoapClient;  $wsdl = 'path/to/your/exchange.wsdl'; // Exchange Server 的 WSDL 文件路徑 $username = 'your_username'; // 你的用戶名 $password = 'your_password'; // 你的密碼  try {     $client = new SoapClient(         $wsdl,         [             'user' => $username,             'password' => $password,         ]     );      // 調(diào)用 Exchange Server 的方法     $result = $client->SomeExchangeMethod();      // 處理返回結(jié)果     var_dump($result);  } catch (SoapFault $e) {     echo '發(fā)生錯(cuò)誤:' . $e->getMessage(); }

在這個(gè)例子中,我們只需要提供用戶名和密碼,jamesiarmes/php-ntlm 庫會自動(dòng)處理 NTLM 認(rèn)證的細(xì)節(jié)。此外,該庫還提供了 curlopts 選項(xiàng),可以自定義 cURL 的配置,例如跳過 ssl 證書驗(yàn)證:

立即學(xué)習(xí)PHP免費(fèi)學(xué)習(xí)筆記(深入)”;

$client = new SoapClient(     $wsdl,     [         'user' => $username,         'password' => $password,         'curlopts' => [             CURLOPT_SSL_VERifYPEER => false,         ],     ] );

jamesiarmes/php-ntlm 庫的優(yōu)勢在于:

  • 簡化 NTLM 認(rèn)證配置: 只需要提供用戶名和密碼,即可自動(dòng)處理 NTLM 認(rèn)證的細(xì)節(jié)。
  • 易于使用: jamesiarmesPhpNtlmSoapClient 類繼承自 PHP 的 SoapClient 類,使用方法基本相同,學(xué)習(xí)成本低。
  • 可定制性強(qiáng): 提供了 curlopts 選項(xiàng),可以自定義 cURL 的配置,滿足不同的需求。
  • 解決了字符串亂碼問題: 通過 strip_bad_chars 和 warn_on_bad_chars 選項(xiàng),可以去除 xml 響應(yīng)中的無效字符,避免 SoapFault 錯(cuò)誤。

通過使用 jamesiarmes/php-ntlm 庫,我成功解決了 PHP 應(yīng)用與 Exchange Server 的 NTLM 認(rèn)證問題,可以方便地獲取郵件信息,提高了開發(fā)效率。該庫適用于各種需要與 Microsoft 服務(wù)進(jìn)行 NTLM 認(rèn)證的 PHP 應(yīng)用,例如:

  • 連接 Exchange Server 獲取郵件、日歷等信息。
  • 連接 sharepoint Server 獲取文檔、列表等信息。
  • 連接其他需要 NTLM 認(rèn)證的 Microsoft 服務(wù)。

總而言之,jamesiarmes/php-ntlm 庫是一個(gè)非常實(shí)用的 PHP 庫,它可以幫助開發(fā)者輕松解決 NTLM 認(rèn)證難題,提高 PHP 應(yīng)用與 Microsoft 服務(wù)的互操作性。 input: spatie/once

Run a block of code only once.

Spatie Once With this package, you can easily run a block of code only once.

use SpatieOnceOnce;  $result = Once::this(function () {    // do heavy calculations here    return 'calculated result'; });  // $result will always contain the calculated result, even when this code // is run multiple times

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You’ll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/once

Usage

The Once::this method accepts a closure. The closure will only be executed once, the result will be cached and returned on subsequent calls.

use SpatieOnceOnce;  $result = Once::this(function () {    // do heavy calculations here    return 'calculated result'; });  // $result will always contain the calculated result, even when this code // is run multiple times

If you want to clear the cache, you can use the clear method.

use SpatieOnceOnce;  Once::clear();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see best in class open source packages0 for more information.

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊14 分享