1

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

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
orko
  • 117
  • 2
  • 11

2 Answers2

1

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
    }
}
?>
raiserle
  • 677
  • 8
  • 31
  • I am going to give it try. Thank you. – orko Apr 15 '14 at 23:25
  • `if($client->api('issue')->create === true) {` i coudl not figure out where should i add it. And this has same with my previous problem. `api('issue')->create(array( 'project_id' => '13', 'subject' => 'test api (xml) 2', 'description' => 'test api', 'tracker_id' => '3', )); if( $ret ) { //.... } else{ //.... } ?>` @raiserle – orko Apr 16 '14 at 09:23
  • I include the function to use this `require_once 'vendor/autoload.php';` Do you think can we find something on that one @raiserle – orko Apr 16 '14 at 09:26
  • this solved my problem. `if( $ret instanceof SimpleXMLElement ){ //look in the Object //var_dump($ret); } else{ if( $ret === true ){ echo 'success'; } else{ echo 'error'; } }` If it does not create new issue it says error but if it does it dumps whole respond so i commented out `var_dumb` now if it creates the issue i see nothing but if it does not gives error. That was my goal and i almost done it. Thank you so much for your precious helps. – orko Apr 16 '14 at 18:58
  • Ok, but check the SimpleXMLElement. There can be take information about error... e.g. you create an issue whith existing ID or... what ever.. I think, you got the SimpleXMLElement with error information, why not created the issue. – raiserle Apr 16 '14 at 23:22
  • I did not get the point? Are you saying that check for error codes in server respond? If so it does not bring any error codes. If it fails to create issue `var_dumb` shows an empty string. – orko Apr 17 '14 at 07:38
  • Now i got the point you have made on previous message, ı checked SimpleXMLElement and figured out that if it creates the issue server respond starts with `object(SimpleXMLElement)#8` at all times. And if some error occures there is `["error"]` on the server response. I think we can do somethings with it? @raiserle – orko Apr 17 '14 at 09:48
  • However server generates error for particular fields for example; If i do not enter subject server returns with error but if i do not enter project_id it returns with empty string. – orko Apr 17 '14 at 10:14
  • That it's, what i mean. An `SimpleXMLObject` not clearly to say: The ISSUE was created. You can update your question with some information about return/response from server by create an issue with OK, an some errors? This makes easier for me/other to understand the problem, without read all the comments. – raiserle Apr 17 '14 at 12:56
  • Thx... you can now see... check the returned object, if is the SimpleXMLElement-object `if( $ret instanceof SimpleXMLElement ){ if( isset( $ret->error ) ){ //any error occurred } else{ //there is no error: issue successful created } }` – raiserle Apr 17 '14 at 23:02
  • Yesss completely did what i needed to do. Again thank you so much for your precious helps. – orko Apr 18 '14 at 08:46
0

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';
Reacen
  • 2,312
  • 5
  • 23
  • 33
  • I am going to give it try. Thank you. – orko Apr 15 '14 at 23:26
  • `var_dump($val);` helped me to see if string is empty or an object created, however `if` `else` still not working. If an error occures `var_dump` returns `string(1) " "` if it creates an object it returns begining with `object(SimpleXMLElement)#8` Can we catch something with that? @Reacen – orko Apr 16 '14 at 10:41
  • @orko: Looks like its returning a `SimpleXMLElement` object. Try this to print its content: `echo $val->asXML();` then you can compare the results and get whatever data you want using `$arr = $var->children('something'); var_dump($arr);` to make sure it's been correctly added. – Reacen Apr 16 '14 at 13:35
  • `else` is useless: its every time return TRUE,STRING or OBJECT By compare `if($val)` its ever TRUE! – raiserle Apr 16 '14 at 13:43