在开发过程中,我需要使用 PHP 应用与公司的 Exchange Server 进行交互,获取邮件信息。然而,Exchange Server 使用 NTLM 认证,而 PHP 原生的 SoapClient 在处理 NTLM 认证时非常麻烦,需要手动设置 cURL 选项,并且容易出错。我尝试了多种方法,但都未能找到一个简单易用的解决方案。
经过一番搜索,我发现了 jamesiarmes/php-ntlm 库。它专门用于处理 php 应用与 microsoft 服务的 ntlm 认证问题,提供了一个易于使用的 soapclient 扩展类,可以简化 ntlm 认证的配置过程。
Composer在线学习地址:学习地址
使用 Composer 安装 jamesiarmes/php-ntlm 库非常简单:
composer require jamesiarmes/php-ntlm
安装完成后,就可以使用 \jamesiarmes\PhpNtlm\SoapClient 类来连接 Exchange Server 了。以下是一个简单的示例:
use jamesiarmes\PhpNtlm\SoapClient;
$wsdl = 'path/to/your/exchange.wsdl'; // Exchange Server 的 WSDL 文件路径
$username = 'your_username'; // 你的用户名
$password = 'your_password'; // 你的密码
try {
$client = new SoapClient(
$wsdl,
[
'user' => $username,
'password' => $password,
]
);
// 调用 Exchange Server 的方法
$result = $client->SomeExchangeMethod();
// 处理返回结果
var_dump($result);
} catch (\SoapFault $e) {
echo '发生错误:' . $e->getMessage();
}在这个例子中,我们只需要提供用户名和密码,jamesiarmes/php-ntlm 库会自动处理 NTLM 认证的细节。此外,该库还提供了 curlopts 选项,可以自定义 cURL 的配置,例如跳过 SSL 证书验证:
立即学习“PHP免费学习笔记(深入)”;
$client = new SoapClient(
$wsdl,
[
'user' => $username,
'password' => $password,
'curlopts' => [
CURLOPT_SSL_VERIFYPEER => false,
],
]
);jamesiarmes/php-ntlm 库的优势在于:
- 简化 NTLM 认证配置: 只需要提供用户名和密码,即可自动处理 NTLM 认证的细节。
-
易于使用:
\jamesiarmes\PhpNtlm\SoapClient类继承自 PHP 的SoapClient类,使用方法基本相同,学习成本低。 -
可定制性强: 提供了
curlopts选项,可以自定义 cURL 的配置,满足不同的需求。 -
解决了字符串乱码问题: 通过
strip_bad_chars和warn_on_bad_chars选项,可以去除 XML 响应中的无效字符,避免SoapFault错误。
通过使用 jamesiarmes/php-ntlm 库,我成功解决了 PHP 应用与 Exchange Server 的 NTLM 认证问题,可以方便地获取邮件信息,提高了开发效率。该库适用于各种需要与 Microsoft 服务进行 NTLM 认证的 PHP 应用,例如:
- 连接 Exchange Server 获取邮件、日历等信息。
- 连接 SharePoint Server 获取文档、列表等信息。
- 连接其他需要 NTLM 认证的 Microsoft 服务。
总而言之,jamesiarmes/php-ntlm 库是一个非常实用的 PHP 库,它可以帮助开发者轻松解决 NTLM 认证难题,提高 PHP 应用与 Microsoft 服务的互操作性。
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 Spatie\Once\Once;
$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 timesSupport 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 Spatie\Once\Once;
$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 timesIf you want to clear the cache, you can use the clear method.
use Spatie\Once\Once; 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.











