download1002 2013-04-08 12:28
浏览 98

使用PHP和Curl通过EWS连接到Microsoft Exchange 2007

In using PHP - Curl - and Soapui (to construct and validate the necessary soap/xml script to send in the Curl Postfields) I find that all I get returned when trying to, for example, add a contact to the default contacts folder are the wsdl definitions in xml format. I believe I have resolved some of my earlier issues by using the Curl code set up as:

$ch = curl_init('https://'.$host.'/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, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8' , 'Expect:')); 
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET" ); *this resolves the 405 http code return*
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlstr);
$response = curl_exec($ch);

The data in $xmlstr is:

$xmlstr = <<<XML
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
  <soap:Body>
      <CreateItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
         <SavedItemFolderId xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
            <DistinguishedFolderId Id="contacts" xmlns="http://schemas.microsoft.com/exchange/services/2006/types"/>
         </SavedItemFolderId>
         <Items xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
            <Contact xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
               <FileAs>Friend A</FileAs>
               <GivenName>Don</GivenName>
               <CompanyName>AdventureWorks</CompanyName>
               <EmailAddresses>
                  <Entry Key="EmailAddress1">don@example.com</Entry>
               </EmailAddresses>
            </Contact>
         </Items>
      </CreateItem>
   </soap:Body>
</soap:Envelope>
XML;

The response/return from the server is:

HTTP/1.1 200 OK
Content-Type: text/xml
Last-Modified: Tue, 05 Feb 2013 23:00:32 GMT
Accept-Ranges: bytes
ETag: "0685999f43ce1:0"
Server: Microsoft-IIS/7.5
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Mon, 08 Apr 2013 10:13:46 GMT
Content-Length: 101206

followed by the xml formatted definitions from the wsdl file.

I would be thankful for some direction on this. I am experienced in several coding languages, but soap/xml and the Curl component in PHP is not within my current knowledge.

  • 写回答

1条回答 默认 最新

  • dongwu9647 2013-04-09 09:05
    关注

    I have now worked out the problem. Essentially the URL I used is incorrect. The Curl_init should be

    $ch = curl_init('https://'.$host.'/EWS/Exchange.asmx');
    

    and the setopts should be changed to:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8')); 
    curl_setopt($ch, CURLOPT_POST, true);
    

    and you don't need:

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET" );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退