【问题标题】:If return false does not work on Redmine API through kbsali/php-redmine-api如果通过 kbsali/php-redmine-api 返回 false 在 Redmine API 上不起作用
【发布时间】:2014-04-15 23:12:25
【问题描述】:

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

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

但是,如果已创建问题,我会在 var_dump 的帮助下看到 SimpleXMLObjectcontent。

代码如下:

    // ----------------------------
// 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

【问题讨论】:

  • 有什么错误吗?更多信息!
  • 'tracker_id' => '3' 后面的逗号有必要吗?
  • @ShaneA.Darr array(1,2,3,4,);是对的!
  • 不,我没有错误,如果每个数据都正确发送它会在 redmine 上产生问题,如果不是很明显它不会。但不管它说什么错误。没有别的了。
  • 好的,请阅读我的帖子:我想,您使用函数作为属性。将 error_level 更改为所有 error_reporting( E_ALL ); 给这个错误?

标签: php xml api redmine redmine-api


【解决方案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
    }
}
?>

【讨论】:

  • 我要试一试。谢谢。
  • if($client-&gt;api('issue')-&gt;create === true) { 我不知道应该在哪里添加它。这与我之前的问题相同。 &lt;?php $ret = $client-&gt;api('issue')-&gt;create(array( 'project_id' =&gt; '13', 'subject' =&gt; 'test api (xml) 2', 'description' =&gt; 'test api', 'tracker_id' =&gt; '3', )); if( $ret ) { //.... } else{ //.... } ?&gt;@raiserle
  • 我包含了使用这个require_once 'vendor/autoload.php';的功能你觉得我们能在那个@raiserle上找到什么吗
  • 这解决了我的问题。 if( $ret instanceof SimpleXMLElement ){ //look in the Object //var_dump($ret); } else{ if( $ret === true ){ echo 'success'; } else{ echo 'error'; } } 如果它没有创建新问题,它会说错误,但如果它确实会转储整个响应,所以我现在注释掉 var_dumb 如果它创建了问题,我什么也看不到,但如果它没有给出错误。那是我的目标,我几乎做到了。非常感谢您的宝贵帮助。
  • 好的,但是检查 SimpleXMLElement。可以获取有关错误的信息...例如您使用现有 ID 或...创建问题。我认为,您得到了带有错误信息的 SimpleXMLElement,为什么不创建问题。
【解决方案2】:

试试这个:

$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';

【讨论】:

  • 我要试一试。谢谢。
  • var_dump($val); 帮助我查看字符串是否为空或创建了对象,但是 if else 仍然无法正常工作。如果发生错误var_dump 返回string(1) " " 如果它创建了一个对象,它返回以object(SimpleXMLElement)#8 开头我们能抓住它吗? @Reacen
  • @orko:看起来它返回了一个SimpleXMLElement 对象。试试这个来打印它的内容:echo $val-&gt;asXML(); 然后你可以比较结果并使用$arr = $var-&gt;children('something'); var_dump($arr); 获取你想要的任何数据,以确保它被正确添加。
  • else 没用:它每次都返回 TRUE、STRING 或 OBJECT 通过比较 if($val) 它永远是 TRUE!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多