如果返回false不能通过kbsali / php-redmine-api在Redmine API上运行

时间:2014-04-15 23:12:25

标签: php xml api redmine redmine-api

我在@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

2 个答案:

答案 0 :(得分: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
    }
}
?>

答案 1 :(得分: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';