Jump to content
Sign in to follow this  
HeyAwesomePeople

Converting variable into a string

Recommended Posts

Hello!

 

I know this seems like an easy task, using strval or casting to string, but the API I am using WON'T accept anything but a hard coded string "". I figure there has to be a way to get around this...

 

What I have here is a method used to convert an array in to a YAML/String format for me to upload onto a server using JSONAPI, found here. I am using Spyc, as that is the only thing I have found that works for this, and it does pretty well.

function pushArrayToServer($array, $api) {
$yaml = Spyc::YAMLDump($array,4,60);

$value = 0;
while (strpos($yaml, "$value:") !== false) {
    $yaml = str_replace("$value:", "-", $yaml);
    $value = $value + 1;
}
$yaml = str_replace("---", "", $yaml); // Final YAML from Array
var_dump($api->call("files.write", array("plugins/GroupManager/worlds/world/groups.yml", $yaml)));
}

Now the problem is that last $api->call I do. This call accepts the method type as a string(argument 1), and an array for argument two. For the method "files.write", the api requires an array being (file location, string). The string is what will be replacing the content of the file. But the only way I can get that line to work is if I do this:

var_dump($api->call("files.write", array("plugins/GroupManager/worlds/world/groups.yml", "a string here")));

That works 100% fine, no errors. This is the dump I get when I run that. But as soon as I do one of these:

var_dump($api->call("files.write", array("plugins/GroupManager/worlds/world/groups.yml", $yaml)));
$yaml2 = $yaml . "";
var_dump($api->call("files.write", array("plugins/GroupManager/worlds/world/groups.yml", $yaml2)));
var_dump($api->call("files.write", array("plugins/GroupManager/worlds/world/groups.yml", strval($yaml))));
var_dump($api->call("files.write", array("plugins/GroupManager/worlds/world/groups.yml", "$yaml2")));
var_dump($api->call("files.write", array("plugins/GroupManager/worlds/world/groups.yml", (string)$yaml)));

These do not work. The dumps all return null. I haven't found a way yet to not do "". It almost worked once when I attempted some things, but I found out I was just converting a boolean into a string which caused an error. The server I am working with is a minecraft server with JSONAPi installed. It works great, except for this error. So I am assuming the string type has to be just like Java's, or pretty plain? I have no clue this is the first real issue I've had with this plugin.

 

Thanks in advance,

HeyAwesomePeople

Share this post


Link to post
Share on other sites

You are definitely passing a string, so the problem must lie within the value of $yaml itself.

Share this post


Link to post
Share on other sites

You are definitely passing a string, so the problem must lie within the value of $yaml itself.

Maybe, but what do I do then?

 

Printing $yaml into a text file results in what I want.

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $yaml);
fclose($myfile);

Result: http://hastebin.com/ucurevadup.sm

Share this post


Link to post
Share on other sites

And now what happens if you put that all into a literal string and call the API with it directly? You should get the same error. The next question is what did you put in the "a string here" that made the API call work, and how does it differ with $yaml?

Share this post


Link to post
Share on other sites

And now what happens if you put that all into a literal string and call the API with it directly? You should get the same error. The next question is what did you put in the "a string here" that made the API call work, and how does it differ with $yaml?

 

THERE'S PARENTHESIS. Ooohhh.....

 

Okay so I need those parenthesis in the YAML to make it work right, so how would I avoid creating an issue with those?

Share this post


Link to post
Share on other sites

THERE'S PARENTHESIS. Ooohhh.....

 

Okay so I need those parenthesis in the YAML to make it work right, so how would I avoid creating an issue with those?

Quotations *

 

Not parenthesis...

Share this post


Link to post
Share on other sites

What quotes?

 

The script fails because of the "" and '' in the yaml file... see here

At least this is what I am assuming, because I can paste any part of the file but I have to escape the quotations. This works but then I have backslashes in my yaml file...

 

Okay scratch what I just said here ^

If I use a literal string, like so,

var_dump($api->call("files.write", array("plugins/GroupManager/worlds/world/groups.yml", "
groups:
    Owner:
        default: false
        permissions: \"\"
        - \'*\'
        inheritance: \"\"
        - Coowner
        info:
            prefix: \'&4[Owner]\'
            build: true
            suffix: \"\"
    HeadAdmin:
        default: false
        permissions: [ ]
        inheritance: \"\"
        - g:server_head_admin
        - admin
        info:
            prefix: \'&c[Head Admin]\'
            build: true
            suffix: \"\"
    Admin:
        default: false
        permissions: [ ]
        inheritance: \"\"
        - g:bukkit_admin
        - g:towny_admin
        - g:groupmanager_admin
        - g:server_admin
        - Architect
        info:
            prefix: \'&c[Head Admin]\'
            build: true
            suffix: \"\"
    Architect:
        default: false
        permissions: [ ]
        inheritance: \"\"
        - g:server_architect
        - HeadModerator
        info:
            prefix: \'&a[Architect]\'
            build: true
            suffix: \"\"
    HeadModerator:
        default: false
        permissions: [ ]
        inheritance: \"\"
        - g:server_head_moderator
        - Moderator
        info:
            prefix: \'&5[Head Moderator]\'
            build: true
            suffix: \"\"
    Moderator:
        default: false
        permissions: [ ]
        inheritance: \"\"
        - g:bukkit_moderator
        - g:towny_moderator
        - g:server_moderator
        - Helper
        info:
            prefix: \'&d[Moderator]\'
            build: true
            suffix: \"\"
    Helper:
        default: false
        permissions: [ ]
")));

This works fine(Other than the fact it adds backslashes to the yaml). The character count of the string is 1622 characters(this includes spaces). But if I add ANY character, just one more space or letter or number, the code fails and outputs this in its var_dump:

null

At 1622 characters I get a success message.

array (size=1)
  0 => 
    array (size=4)
      'result' => string 'success' (length=7)
      'success' => boolean true
      'source' => string 'files.write' (length=11)
      'is_success' => boolean true

So at first I think there's a limit of 1622 characters, but that's a very specific number. So I put into the literal string 3000 random characters and it worked....

 

I'm lost here. The difference between working and not working is 1 character...?

 

I did another test too. I cut the yaml file I am editing to around 1550 characters and my script works perfectly fine all the way through. I figured this would be the case, but this means the quotes don't have anything to do with it.

 

 

So I'm realllllyyyy confused. In the YAML format, I can only hit 1622 characters. Is there a php limit somewhere I don't know about?

Edited by HeyAwesomePeople

Share this post


Link to post
Share on other sites

The backslash problem is because you're putting backslashes in places that don't need it. Since the backslashes aren't necessarily there, PHP keeps them in place thinking that you wanted literal backslashes. Don't use them for single quotes in a double-quoted string, or vice versa.

 

Whatever you're experiencing is not due to PHP. I'm more troubled by the "3000 random characters and it worked" statement, which must mean something besides other than what those words literally mean.

 

This is probably the time when you should be looking more closely at the API itself. If the code returns null then look to see under what conditions it will return null. Trace it back up to the actual API call (or whatever) and find out exactly what's happening. Because poking and prodding with different strings here and there isn't making much progress.

Share this post


Link to post
Share on other sites

The backslash problem is because you're putting backslashes in places that don't need it. Since the backslashes aren't necessarily there, PHP keeps them in place thinking that you wanted literal backslashes. Don't use them for single quotes in a double-quoted string, or vice versa.

 

Whatever you're experiencing is not due to PHP. I'm more troubled by the "3000 random characters and it worked" statement, which must mean something besides other than what those words literally mean.

 

This is probably the time when you should be looking more closely at the API itself. If the code returns null then look to see under what conditions it will return null. Trace it back up to the actual API call (or whatever) and find out exactly what's happening. Because poking and prodding with different strings here and there isn't making much progress.

 

If I don't escape the "" and '' I get php errors.

Share this post


Link to post
Share on other sites

If I don't escape the "" and '' I get php errors.

One of them. You escape one of them. The one that you are using for the string delimiters. Not both of them.

Share this post


Link to post
Share on other sites

One of them. You escape one of them. The one that you are using for the string delimiters. Not both of them.

 

Oh yeah... Okay so escaping just the "" doesn't result in an error, I just get the null message again.

 

And the API is pretty simple. https://github.com/alecgorge/jsonapi   http://hastebin.com/jozigaruru.php

The API provides a way to communicate between a Minecraft server, Java, and the user. This can be through web app or iphone/android app or whatever. I don't think it's the API's fault, as Adminium(IOS app) also has a file manager system using the JSONAPI, and their files work just fine(including the types of one I am messing with).

 

I looked through the Java anyways and found the method I am calling through the website: http://hastebin.com/avodositud.coffee

Now this method only returns a success message or an error message, not nothing. It also state here that it should only return true or an error message... I'm also not the best at PHP(still fairly new to it) so the JSONAPI php file is hard to read for me, but I don't see anywhere I could get an error..

 

I cannot figure this out still. Thanks for helping me so far. What else should I be trying/looking at?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.