php - 使用 PHP 和 cURL 访问 Exchange Web 服务

标签 php curl ntlm exchangewebservices http-status-code-401

你好,

我目前正在编写一个客户端来访问 Microsoft Exchange 服务器并从中读取联系人、约会等。

经过几天的搜索,我已经能够通过 PHP 的 Soap 客户端和自定义 HTTPS Stream 包装器连接到 EWS。 This网站在这一点上给了我很大帮助。

使用 XAMPP 在我的 Windows 7 计算机上一切正常

现在我将我的项目上传到 Debian 6.0 Squeeze 开发机器,该机器在 Web 服务器、php 设置、mysql 设置等方面与我的 Windows 机器具有完全相同的配置,但它不再工作了

debian 机器可以毫无问题地解析并 ping 通 Exchange 服务器

我将实际问题归结为 cURL 无法检索 EWS 的 WSDL 文件

它总是收到空响应和 401(未经授权)状态代码

我使用的凭据是正确的,相同的凭据在我的 Windows 计算机上有效

我提取了有问题的代码并尝试独立运行它,它看起来像这样:

    echo "Trying to get https://".$cfg[ 'Exchange.Server' ]."/EWS/Services.wsdl<br>";
    $curl = curl_init( 'https://'.$cfg[ 'Exchange.Server' ].'/EWS/Services.wsdl' );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER,     true );
    curl_setopt( $curl, CURLOPT_HTTP_VERSION,       CURL_HTTP_VERSION_1_1 );
    curl_setopt( $curl, CURLOPT_HTTPAUTH,           CURLAUTH_NTLM );
    curl_setopt( $curl, CURLOPT_USERPWD,            $cfg[ 'Exchange.User' ].':'.$cfg[ 'Exchange.Password' ] );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER,     false );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST,     false );

    echo '<pre>';
    $response = curl_exec( $curl );
    $info = curl_getinfo( $curl );

    var_dump( $info );
    var_dump( $response );

    curl_close( $curl );

我在这里收到的结果是提到的 401 状态代码和空响应 当我在浏览器中调用相同的 URL 或在 Windows 计算机上使用相同的代码时,我会得到我想要的 WSDL 文件

实际上,我什至无法判断这是一个基于 Linux 的问题,还是我在某个时候做错了什么,我已经为此苦苦挣扎了 2 天。

是否有人能够发现我的错误或告诉我它不起作用的原因?

我可以根据需要提供任何进一步需要的信息

最佳答案

如果正确初始化你的soap客户端,你应该能够以这种方式执行任何请求:

$curl = curl_init($location); //'https://'.$server_address.'/EWS/Exchange.asmx'
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //valid soap headers with keep-alive
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
$response = curl_exec($curl);

对于您的代码,请尝试注释掉以下行:

curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM );

另请注意此包装器在您的设置中的任何位置工作: http://ewswrapper.lafiel.net/ 如果是这样,请查看那里使用的 SOAP 类 - 它使用 php 内置作为基础。

为什么你不能在本地存储 wsdl 文件?

<小时/>

更新:

好吧,我在我的 Debian 盒子里尝试过这个,这对我来说完美无缺:

$domain = 'xxxxxx';
$user = 'xxxxxx';
$password = 'xxxxxx';
$ch = curl_init('https://'.$domain.'/EWS/Services.wsdl'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
$response = curl_exec($ch);
$info = curl_getinfo( $ch );
$error =  curl_error ($ch);
print_r(array($response,$info,$error));

返回

Array
(
    [0] => <?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions 
(...)
</wsdl:definitions>
    [1] => Array
        (
            [url] => xxxxx/EWS/Services.wsdl
            [content_type] => text/xml
            [http_code] => 200
            [header_size] => 250
            [request_size] => 147
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 0.60574
            [namelookup_time] => 0.165249
            [connect_time] => 0.268173
            [pretransfer_time] => 0.474009
            [size_upload] => 0
            [size_download] => 55607
            [speed_download] => 91800
            [speed_upload] => 0
            [download_content_length] => 55607
            [upload_content_length] => 0
            [starttransfer_time] => 0.580931
            [redirect_time] => 0
            [certinfo] => Array
                (
                )

            [redirect_url] => 
        )

    [2] => 
)

关于php - 使用 PHP 和 cURL 访问 Exchange Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7664249/

相关文章:

php - 为什么 geoip 会抛出异常?

php - 发送 $_POST 请求后,让curl 与响应头的 Content-Disposition 一起工作

node.js - 将 http 请求从 cURL 转换为 Node http.request

authentication - 如何在 Apache Nutch 中进行 NTLM 身份验证?

带有 NTLM 身份验证的 SOAP Web 服务的 Java 客户端

curl - NTLM 和 Golang

php - ob_flush() 在本地主机上工作但不在 GoDaddy 上在线

php - 无法在外部 dbgetaddrinfo 上连接 Wordpress 失败 : Name or service not known

php - PHP 购物车中买一送半价总计

php - cURL 抓取给我 'Request Rejected' 请求的 URL 被拒绝