使用PHP和cURL访问Exchange Web服务 [英] Access Exchange Web Services with PHP and cURL

查看:252
本文介绍了使用PHP和cURL访问Exchange Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



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



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

在我使用XAMPP的Windows 7机器上一切正常工作



现在我将项目上传到Debian 6.0 Squeeze开发机器有与我的Windows机器完全相同的配置关于网络服务器,php设置,mysql设置等,但它只是不再工作



debian机器可以解决并且没有问题地ping交换服务器



我将实际问题锁定到某一点,其中cURL无法检索EWS的WSDL文件



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



我使用的凭证是正确的,在我的Windows机器上工作



我提取了错误的代码段,并尝试独立运行它,它看起来像这样:

  echo尝试获取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文件?





$ b b

UPDATE:



好吧,我在我的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.0encoding =utf-8?>
< wsdl:definitions
(...)
< / wsdl:definitions>
[1] => Array

[url] => xxxxx / EWS / 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] =>数组



[redirect_url] =>


[2] =>


Hello,

I am currently writing a client to access a Microsoft Exchange server and read contacts, appointments etc. from it.

Through days of searching I've been able to connect to the EWS via PHP's Soap client and a custom HTTPS Stream wrapper. This website helped me greatly at this point.

Everything worked fine on my Windows 7 machine using XAMPP

Now I uploaded my project to a Debian 6.0 Squeeze development machine that has exactly the same configuration as my Windows machine regarding the web-server, php settings, mysql settings etc. but it just wont work anymore

The debian machine can resolve and ping the exchange server without problems

I nailed the actual problem down to a point, where cURL isn't able to retrieve the WSDL file of the EWS

It always receives an empty response and a 401 (Unauthorized) status code

The credentials I use are correct, the same credentials work on my windows machine

I extracted the faulty piece of code and tried running it stand-alone, it looks like this:

    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 );

The result I receive here is the mentioned 401 status code and an empty response When I call the same url in my browser or with the same code on my windows machine, I get the WSDL file I want

Actually I can't even tell if this is a linux-based problem or if I do something wrong at some point, I'm struggling with this for 2 days now.

Is there someone that may be able to find my mistake or tell me the reason why it doesn't work?

I may provide any further needed information on demand

解决方案

If you initialize your soap client properly, you should be able to preform any requests requests this way:

$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);

As to your code, try commenting out following line:

curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM );

Also take a look wherever this wrapper works on your setup: http://ewswrapper.lafiel.net/ If it does, take a look at SOAP classes used there - it uses php built-in as base.

Why can't you store wsdl file locally anyways?


UPDATE:

Ok, I played around with this in my Debian box and this works for me flawlessly:

$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));

returns

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和cURL访问Exchange Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆