1

我在@raiserle 的帮助下更新了代码(谢谢)。

如果用户无权创建问题或存在连接问题,下面的代码使我能够显示错误消息,最后但并非最不重要的是如果project_id为空或无效。

但是,如果已创建问题,我SimpleXMLObject会在var_dump.

这是代码:

    // ----------------------------
// Instanciate a redmine client
// --> with ApiKey
$client = new Redmine\Client('http://localhost:8080/redmine/', '210940249ksdjfksdh32');

// ----------------------------
// [OPTIONAL] set the port
// (it will try to guess it from the url)
$client->setPort(8080);

// ----------------------------
$ret = $client->api('issue')->create(array(
    'project_id'        => '',
    'subject'           => $subject,
    'description'       => $description,
    'assigned_to_id'    => $assingto,
    'tracker_id'        => $trackerId,
    'watcher_user_ids'  => $watcherId,
));
if( $ret instanceof SimpleXMLElement ){
    //look in the Object
    var_dump($ret);
}
else{
    if( $ret === true ){
        echo "success";
    }
    else{
       echo "error";
    }
}

现在的问题是我可以根据错误发出简单的成功消息或错误消息。

这是服务器响应/返回示例。服务器返回以下错误,假设我没有提供主题:

object(SimpleXMLElement)#8 (2) {
  ["@attributes"]=>
  array(1) {
    ["type"]=>
    string(5) "array"
  }
  ["error"]=>
  string(22) "Subject can't be blank"
}

如果问题已成功创建,这是服务器响应的示例:

object(SimpleXMLElement)#8 (17) {
  ["id"]=>
  string(3) "340"
  ["project"]=>
  object(SimpleXMLElement)#6 (1) {
    ["@attributes"]=>
    array(2) {
      ["id"]=>
      string(1) "9"
      ["name"]=>
      string(26) "Some Project name"
    }
  }
  ["tracker"]=>
  object(SimpleXMLElement)#9 (1) {
    ["@attributes"]=>
    array(2) {
      ["id"]=>
      string(1) "4"
      ["name"]=>
      string(6) "Some tracker name"
    }
  }
  ["status"]=>
  object(SimpleXMLElement)#10 (1) {
    ["@attributes"]=>
    array(2) {
      ["id"]=>
      string(1) "1"
      ["name"]=>
      string(4) "New"
    }
  }
  ["priority"]=>
  object(SimpleXMLElement)#11 (1) {
    ["@attributes"]=>
    array(2) {
      ["id"]=>
      string(1) "2"
      ["name"]=>
      string(6) "Normal"
    }
  }
  ["author"]=>
  object(SimpleXMLElement)#12 (1) {
    ["@attributes"]=>
    array(2) {
      ["id"]=>
      string(2) "22"
      ["name"]=>
      string(7) "author name"
    }
  }
  ["assigned_to"]=>
  object(SimpleXMLElement)#13 (1) {
    ["@attributes"]=>
    array(2) {
      ["id"]=>
      string(2) "10"
      ["name"]=>
      string(6) "Some name"
    }
  }
  ["subject"]=>
  string(16) "test api (xml) 2"
  ["description"]=>
  string(25) "some dummy content"
  ["start_date"]=>
  string(10) "2014-04-17"
  ["due_date"]=>
  object(SimpleXMLElement)#14 (0) {
  }
  ["done_ratio"]=>
  string(1) "0"
  ["estimated_hours"]=>
  object(SimpleXMLElement)#15 (0) {
  }
  ["spent_hours"]=>
  string(3) "0.0"
  ["created_on"]=>
  string(20) "2014-04-17T15:52:07Z"
  ["updated_on"]=>
  string(20) "2014-04-17T15:52:07Z"
  ["closed_on"]=>
  object(SimpleXMLElement)#16 (0) {
  }
}

任何帮助都会得到很大的帮助。只是引导我正确的方式。除此之外,kbsali 并没有过多地说明他们代码的使用。我不知道是否有办法从他们的代码中获取服务器响应。我的意思是显然代码得到了服务器响应,但我不知道如何才能达到它。如果有人知道这也肯定会解决我的问题。

这是 github 上 kbsali redmine-php-api 的 URL: https ://github.com/kbsali/php-redmine-api

4

2 回答 2

1

我到了 github - 看看这个

<?php
$client->api('issue')->create(array(
  'project_id'  => 'test',
  'subject'     => 'some subject',
  'description' => 'a long description blablabla',
  'assigned_to' => 'user1',
));
?>

你做这个

<?php
if($client->api('issue')->create === true) { //create is no property, is a function
?>

将您的代码更改为

<?php
$ret = $client->api('issue')->create(array(
  'project_id'     => '13',
  'subject'        => 'test api (xml) 2',
  'description'    => 'test api',
  'tracker_id'         => '3',
));
if( $ret ) {
    //....
}
else{
    //....
}
?>

我有源代码并提取代码以添加问题,显示在: http: //pastebin.com/dXB1c88S

将 if 语句更改为

<?php

if( $ret instanceof SimpleXMLElement ){
    //look in the Object
    var_dump($ret);

    //UPDATE: after see your response object
    if( isset( $ret->error ) ){
        //any error occurred
        //$ret->error takes an message
    }
    else{
        //there is no error: issue successful created 
    }
}
else{
    if( $ret === true ){
        //i do not see... issue is successful created?
        //there is no response?!
    }
    else{
        //return is a string
        //i do not see... issue is successful created?
        //check the string
    }
}
?>
于 2014-04-15T23:18:11.380 回答
0

试试这个:

$val = $client->api('issue')->create(array(
    'project_id'     => '13',
    'subject'        => 'test api (xml) 2',
    'description'    => 'test api',
    'tracker_id'     => '3'
));

//Debug
var_dump($val);
echo $val->asXML();

//Once you read the XML's content
$var = (string) $var->someNode->innerNode;
if($var==='whatever_is_the_expected_result')
    echo 'data sent';
else
    echo 'error';
于 2014-04-15T23:19:20.507 回答