【问题标题】:Forbidden to Call method on Dynamics 365 AX via PHP Curl禁止通过 PHP Curl 在 Dynamics 365 AX 上调用方法
【发布时间】:2019-08-04 06:52:04
【问题描述】:

我正在尝试通过 PHP curl 通过 WSDL 从动态 SOAP 调用方法。

我的 webapp 和 SOAPUI 都出现此错误。 可能是什么问题呢?从具有相同凭据的 .NET 测试程序访问时,它工作正常。只是面对 PHP 方面的问题,说 Forbidden 和 1317 代码。指定的帐号不存在

我一直在尝试调用该方法并遇到不同的问题,我遇到的最后一个问题是这个。 我想也许用户代理我改变了它我使用了 SOAPUI。一样。 我所知道的是用户已在 Azure AD 中注册,并且应该拥有该应用程序的授权。

POST 是

POST /soap/services/servicemethodname?wsdl 
HTTP/1.1 
Host: domainname.sandbox.ax.dynamics.com 
Accept: text/xml 
Accept-Encoding: gzip,deflate 
Connection: Keep-Alive 
Content-type: text/xml 
User-Agent: Apache-HttpClient 
Authorization: Bearer longTokenString
Soapaction: "http://tempuri.org/webservice/method" 
Content-Length: 795 

响应是

 HTTP/1.1 500 Internal Server Error Cache-Control: private 
 Content-Type: text/xml; charset=utf-8 
 Server: Microsoft-IIS/10.0 
 Strict-Transport-Security: max-age=31536000; includeSubDomains 
 Set-Cookie: ASP.NET_SessionId=hghtgkuhlihkjg; path=/; secure; 
 HttpOnly Set-Cookie: 
 ms-dyn-csrftoken= someTokenSTring; path=/; secure 
 ms-dyn-fqhn: 
 ms-dyn-namespace: namespace 
 ms-dyn-tenant: tenantidstring 
 ms-dyn-role: 
 ms-dyn-aid: aidString 
 X-Powered-By: ASP.NET 
 X-Content-Type-Options: nosniff 
 X-Frame-Options: SAMEORIGIN 
 p3p: CP="No P3P policy defined. Read the Microsoft privacy statement at https://go.microsoft.com/fwlink/?LinkId=271135" 
 Strict-Transport-Security: max-age=31536000; 
 includeSubDomains Date: Thu, 01 Aug 2019 19:24:52 GMT Content-Length: 1112 
 a:ForbiddenForbidden1317System.ComponentModel.Win32ExceptionThe specified account does not exist0-2147467259

我需要能够正确调用该方法并获取它发送的值。

我的php代码

$requestBody = trim('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dat="http://schemas.microsoft.com/dynamics/2013/01/datacontracts" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tem="http://tempuri.org">
   <soapenv:Header>
      <dat:CallContext>
         <dat:Company>company</dat:Company>
         <dat:Language>en-us</dat:Language>
         <dat:MessageId>?</dat:MessageId>
         <dat:PartitionKey>12345667</dat:PartitionKey>
      </dat:CallContext>
   </soapenv:Header>
   <soapenv:Body>
      <m:getMethod xmlns:m="http://tempuri.org/webService/getMethod">
         <m:parameterName soap:mustUnderstand="1">12345</m:parameterName>
      </m:getMethod>
   </soapenv:Body>
</soapenv:Envelope>
            ');

    $soapAction = 'SOAPAction: http://tempuri.org/webService/getMethod';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, 
            array(  'Accept:text/xml',
                    'Accept-Encoding: gzip,deflate',
                    'Connection: Keep-Alive',
                    'Content-type: text/xml; charset=utf-8',
                    'Cache-Control: no-cache',
                    'Pragma: no-cache',
                    'Authorization: Bearer longstringToken',
                    'SOAPAction: http://tempuri.org/webService/getMethod'
                ));
 if ($postData != '') {
            curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);             
        }
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
// By default https does not work for CURL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt ($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
// Set the option to recieve the response back as string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$odataURL = 'https://domainname.sandbox.ax.dynamics.com/soap/services/webService'; 
curl_setopt($ch, CURLOPT_URL, $odataURL);
// enable string response
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ch, CURLOPT_POST, true);   

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
// Mark as Post request
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// $output contains the output string
$output = curl_exec($ch);

【问题讨论】:

    标签: php curl soap authorization dynamics-365


    【解决方案1】:

    好的,终于找到了解决方案。 它有助于阅读有关您使用的类和使用的不同系统的文档。在我的情况下,我试图将我的应用程序与 microsoft dynamics 365 ax 集成,所以我也必须阅读它。

    我阅读了很多文档,其中一些与不同的动态服务有关,但this one helped most

    由于soap服务需要Authorization Header,因为他们使用Windows身份验证,我们需要从oAuth链接获取令牌。

    https://login.windows.net/$tenantDomainName/oauth2/token

    PS:我从githubPHPConsoleApplication 了解到的 oauth2 链接

    我使用 PHP CURL 来获取我的授权令牌,然后使用 PHP 的 SoapClient 类创建了一个客户端。

    确保在标头中添加授权令牌,如下所示:

    $arrayOpt = array(    
    'stream_context'  => stream_context_create(
                                array('http' =>'Authorization: Bearer tokenString')
     ));
    
    $client = new SoapClient($wsdl, $arrayOpt);
    
    $response = $client->serviceMethod($parameters);
    
    var_dump($response);
    

    你会得到方法的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 2021-02-19
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多