Question

I updated the code with help by @raiserle (thank you).

The code below enables me to show an error message if a user has no right to create an issue or there is connection problem and last but not least if project_id is empty or not valid.

However, if the issue has been created I see SimpleXMLObjectcontent with the help of var_dump.

Here is the code:

    // ----------------------------
// 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";
    }
}

Now the thing is can I make a simple success message or error messages according to error.

Here are the server responses/returns samples. Server returns with the following error let`s say if I did not provide a subject:

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

And here is an example of server response if the issues have been created successfully:

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) {
  }
}

Any help will be much approtiated. Just guide me to correct way. Other then that kbsali does not say much about the usage of their code. I do not know if there is a way to get server responses from their codes. I mean obviously code gets server response but I do not know how can I reach it. If anybody figures how that will also solve my problem definitely.

Here is the URL for kbsali redmine-php-api on github: https://github.com/kbsali/php-redmine-api

Was it helpful?

Solution

I'm got to the github - and see this

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

you make this

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

change your code to

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

I've got the source and extract the code to add an issue, shown at: http://pastebin.com/dXB1c88S

change the if-statement to

<?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
    }
}
?>

OTHER TIPS

Give this a try:

$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';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top