문제

I got a problem I cant solve for 7 hours now.

this is my php script for creating user on openfire server:

$f = fopen("LINK","r");
$odpoved = fread($f, 1024);

in manual http://www.igniterealtime.org/projects/openfire/plugins/userservice/readme.html is written that my variable should contain either "OK" or "UserAlreadyExistsException". When i try to print $odpoved, it show good, when i view source of that page it contain tags as written in manual. My problem is, that i cant for gods sake find a way to use this variable in switch. This is how it looks now:

switch($odpoved){
    case 'OK':
        print("something");
        break;
    case 'UserAlreadyExistsException':
        print("something2");
        break;
    default:
        print("X");
}
fclose($f);

No matter what i do, it always print default. I dont understand what is wrong with it. I tried already to compare it to options with tags too, but it didnt help and it always end up in default. Would you kindly help me please? Thanks for any help and if needed I will provide additional details.

도움이 되었습니까?

해결책

The server will reply to all User Service requests with an XML result page. If the request was processed successfully the return will be a "result" element with a text body of "OK". If the request was unsuccessful, the return will be an "error" element with a text body of one of the following error strings.

The output is an XML file, so what you're looking at is using an XML parser to get the result element. simplexml will load it into an array (since this xml page is small you shouldn't have to worry about performance so more complicated xml parsers aren't worth the trouble)

$output = simplexml_load_string($odpoved) 
switch($output->result) //check $odpoved to make sure the XML structure matches

Or, you can also pass the url using simplexml_load_file the output will be the same.

for more information, see: http://www.sitepoint.com/parsing-xml-with-simplexml/

다른 팁

Your switch statement looks fine to me. I would question the content in your variable $odpoved. Use var_dump($odpoved) to see what it contains and be sure to check for leading and trailing spaces or hidden characters (newline chars are sometimes hard to spot).

For switch statement analysis check out http://www.php.net/manual/en/control-structures.switch.php

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top