2011-12-03 115 views
0

我在使用Web服務方面很新穎。感謝有人能幫助我。PHP如何使用SOAP Web服務?

在我的PHP代碼中,我試圖從另一個服務器(JIRA,Java)使用SOAP Web服務。 JIRA SOAP API顯示爲here

$jirasoap = new SoapClient($jiraserver['url']); 
$token = $jirasoap->login($jiraserver['username'], $jiraserver['password']); 
$remoteissue = $jirasoap->getIssue($token, "issuekey"); 

我發現我的代碼沒有問題可以調用該頁面上列出的功能。但是,我不知道如何使用由API調用返回的對象。

我的問題是:

  1. 在我的PHP代碼,我怎麼可以使用通過SOAP API調用返回的Java類對象methods
    例如,功能$remoteissue = $jirasoap->getIssue($a, $b)將返回RemoteIssue。基於此(http://docs.atlassian.com/rpc-jira-plugin/latest/com/atlassian/jira/rpc/soap/beans/RemoteIssue.html),有諸如getSummarygetKey等的方法。我可以在我的代碼中使用這些功能嗎?
    基於一些PHP的例子,我從網上查到,似乎每個人都使用這樣的事情:
    $remoteissue = $jirasoap->getIssue($token, "issuekey");
    $key = $remoteissue->key;
    他們不使用對象的方法。
    參考此example,似乎有人能夠用其他語言來做到這一點。它也可以在PHP中完成嗎?
    我面臨的問題是,我試圖獲取附件的ID。但是,似乎我們無法使用此方法獲得附件ID:$attachmentid = $remoteattachment->id;。我正在嘗試使用$remoteattachment->getId()方法。

  2. 在PHP代碼中,在我們完成SOAP API調用並收到返回的對象之後,我們如何知道該對象中有哪些數據字段可用?
    例如,
    $remoteissue = $jirasoap->getIssue($token, "issuekey");
    $summary = $remoteissue->summary;
    我們怎麼知道->summary可在$remoteissue
    當我參考此文檔(http://docs.atlassian.com/rpc-jira-plugin/latest/com/atlassian/jira/rpc/soap/beans/RemoteIssue.html)時,我沒有看到它提及RemoteIssue中的任何數據字段。我們如何知道我們可以從這個對象中獲得keysummary等?我們怎麼知道它是->summary,而不是->getsummary?我們需要使用Web瀏覽器來打開WSDL URL?

謝謝。

回答

0

您在#2中提到的文檔是針對Java實現的,實際上並沒有給您任何PHP幫助。如果他們沒有爲他們的服務發佈一個公共API(這將是不常見的),那麼使用WSDL作爲參考可以讓你知道服務接受哪些對象和方法,並且可以相應地規劃你的方法調用。

你以前稱爲getIssue(...)的技術看起來不錯,儘管你應該考慮在SoapException的情況下使用try ... catch。

0

我已經使用了Jira SOAP。NET項目和IntelliSense暗示我有哪些字段可用於返回的對象。

如果您使用的是Visual Studio,則可以使用類似VS.Php for Visual StudioPhp for Visual Studio的東西。

或者您可以選擇其中一個支持IntelliSense的here IDE。

2

這個問題已經過了一年多了,但爲了分享知識併爲有同樣問題的人提供了答案,並且找到了這個頁面,下面是我的發現。

問題中提到的文檔是JiraSoapService接口的概述。這對於哪些函數可以用哪些參數以及它們返回什麼來調用是一個很好的參考。

如果您爲Jira SoapClient使用Java,則會實現返回的對象,但是如果使用PHP,則返回的對象不是本文檔中所述的類型,並且沒有提及的任何方法。返回的對象是內部PHP類stdClass的實例,它是未定義對象的佔位符。知道返回內容的最佳方法是對SoapCalls返回的對象使用var_dump()

$jirasoap = new SoapClient($jiraserver['url']); 
$token = $jirasoap->login($jiraserver['username'], $jiraserver['password']); 
$remoteissue = $jirasoap->getIssue($token, "PROJ-1"); 

var_dump($remoteissue); 
/* -- You will get something like this --- 
object(stdClass)#2 (21) { 
    ["id"]=> string(3) "100" 
    ["affectsVersions"]=> array(0) { } 
    ["assignee"]=> string(4) "user" 
... 
    ["created"]=> string(24) "2012-12-13T09:27:49.934Z" 
... 
    ["description"]=> string(17) "issue description" 
.... 
    ["key"]=> string(6) "PROJ-1" 
    ["priority"]=> string(1) "3" 
    ["project"]=> string(4) "PROJ" 
    ["reporter"]=> string(4) "user" 
    ["resolution"]=> NULL 
    ["status"]=> string(1) "1" 
    ["summary"]=> string(15) "Project issue 1" 
    ["type"]=> string(1) "3" 
    ["updated"]=> string(24) "2013-01-21T16:11:43.073Z" 
    ["votes"]=> int(0) 
} 
*/ 

// You can access data like this: 
$jiraKey = $remoteissue->key; 
$jiraProject = $remoteissue->project;