如果返回false,则通过kbsali/php Redmine API在Redmine API上不起作用

如果返回false,则通过kbsali/php Redmine API在Redmine API上不起作用,php,xml,api,redmine,redmine-api,Php,Xml,Api,Redmine,Redmine Api,我在@raiserle的帮助下更新了代码(谢谢) 下面的代码使我能够在用户无权创建问题或存在连接问题时显示错误消息,最后但并非最不重要的是,如果project\u id为空或无效 但是,如果问题已经创建,我会在var\u dump的帮助下查看SimpleXMLObject内容 代码如下: // ---------------------------- // Instanciate a redmine client // --> with ApiKey $client = new R

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

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

但是,如果问题已经创建,我会在
var\u dump
的帮助下查看
SimpleXMLObject
内容

代码如下:

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

我要去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{
    //....
}
?>
“测试api”,
“跟踪器id”=>“3”,
));
如果($ret){
//....
}
否则{
//....
}
?>

我已获得源代码并提取代码以添加问题,如所示:

将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
    }
}
?>
错误){
//出现任何错误
//$ret->error接收消息
}
否则{
//没有错误:已成功创建问题
}
}
否则{
如果($ret==true){
//我看不到…问题是否成功创建?
//没有回应?!
}
否则{
//return是一个字符串
//我看不到…问题是否成功创建?
//检查字符串
}
}
?>
尝试一下:

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

有错误吗?更多信息!“tracker_id”=>“3”后面是否需要逗号?@ShaneA.Darr数组(1,2,3,4,);是的!不,我并没有得到任何错误,若每个数据发送正确,它会在redmine上产生问题,若不是明显的,它不会。但不管它说什么错误。没什么了。好吧,读我的帖子:我想,你用一个函数作为属性。将错误级别更改为所有
错误报告(E\u all)给它一个错误?我要给它一个尝试。谢谢。
if($client->api('issue')->create==true){
我不知道应该在哪里添加它。这与我以前的问题相同。
'test api','tracker_id'=>3',);if($ret){/..}否则{/..}?>
@raiserleI包含使用此函数的函数
require_once'vendor/autoload.php';
您认为我们能在该函数上找到什么吗@raiserleth这解决了我的问题。
if($ret instanceof simplexmlement){//查看对象//var_dump($ret)}else{if($ret true==true){echo'success'}else{echo'错误';}
如果它没有创建新的问题,它会显示错误,但是如果它创建了,它会转储整个响应,所以我注释掉了
var\u dumb
现在如果它创建了问题,我什么也看不到,但是如果它没有给出错误。这是我的目标,我几乎完成了。非常感谢您宝贵的帮助。好的,但是检查simplexmlement。可以获取信息关于错误的说明…例如,您创建了一个带有现有ID的问题,或者…什么都有..我想,您得到了带有错误信息的SimpleXMLElement,为什么不创建该问题。我将尝试一下。谢谢。
var\u dump($val)
帮助我查看字符串是空的还是创建的对象,但是
if
else
仍然不工作。如果发生错误
var\u dump
返回
string(1)”,
如果创建对象,则返回以
对象开头的对象(SimpleXMLElement)#8
我们能用它抓点什么吗@Reacen@orko:看起来它返回了一个
SimpleXMLElement
对象。请尝试打印它的内容:
echo$val->asXML();
然后您可以比较结果,并使用
$arr=$var->children('something');var_dump($arr)获取所需的任何数据;
以确保它被正确添加。
否则
是无用的:它每次通过比较
if($val)
返回TRUE、STRING或OBJECT都是TRUE!