簡體   English   中英

通過PHP Curl在Dynamics 365 AX上禁止調用方法

[英]Forbidden to Call method on Dynamics 365 AX via PHP Curl

我試圖通過PHP curl通過WSDL從Dynamics SOAP調用方法。

我從我的webapp和SOAPUI都收到此錯誤。 可能是什么問題呢? 從具有相同憑據的.NET測試程序訪問時,它可以正常工作。 剛從PHP方面面臨問題,說禁止使用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);

好的,所以終於找到了解決方案。 它有助於閱讀有關您使用的類和使用的不同系統的文檔。 以我為例,我試圖將我的應用程序與Microsoft Dynamics 365 ax集成在一起,因此我也必須閱讀有關內容。

我讀了很多文檔,其中一些與不同的動態服務有關,但這對大多數人有幫助

而且由於soap服務需要授權標頭,因為它們使用的是Windows身份驗證,所以我們需要從oAuth鏈接獲取令牌。

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

PS:我從github PHPConsoleApplication知道的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);

您將獲得該方法的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM