0

I have been having trouble getting a result from this wondered if someone may be able to help me.

I am trying to decode/parse this json output from a URL so that I can extract "result" and "success" and place them into variables. So far I cant even parse the json output and when I try to print $data just returns "unexpected T_VARIABLE".

JSON output:

[{"result":"success","success":"1.7.2","source":"version","tag":"sampleTag","is_success":true}]

My code:

$json = file_get_contents($url);
    $data = json_decode($json, TRUE);
    print_r $data;

Ive done some limited work with json before, and I have never seen an output in square brackets [ ] like this...
Could this be where im going wrong? Any suggestions appricated.

Thanks.

3
Contributors
18
Replies
108
Views
3 Years
Discussion Span
Last Post by NuGG
Featured Replies
  • 1
    cereal 1,419   3 Years Ago

    It could be line `3` instead of the `json_decode` function, `print_r` requires brackets, so change it to: print_r($data); Read More

1

It could be line 3 instead of the json_decode function, print_r requires brackets, so change it to:

print_r($data);

Edited by cereal

0

Hi,

This works

$json = '[{"result":"success","success":"1.7.2","source":"version","tag":"sampleTag","is_success":true}]';

print_r(json_decode($json, TRUE);

I suspect that the error is coming from the url source..

0

Yea I noticed that after I posted it, but no thats not it.

I have been doing some testing and added this code to check that file_get_contents() is actually working...

if ($json === FALSE) {
    echo "Failed to open the URL.";
    } elseif ($json === NULL) {
    echo "Function is disabled.";
    } else {
   echo $json;
    }
  • and it replies with nothing. Meaning its obviously not retrieving the data because it cant echo it, but yet is isnt failing to open the URL either.

  • I have also checked that allow_url_fopen is allowed, which it is.

  • I can confirm that the json output is working because if I just visit the URL I can see it.

Proving to be very fustrating...

0

another option beside allow_url_fopen is to use cURL.

Here is a simple funtion for utilization of cURL

function useCurl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');  
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch); 

    return($output);
    unset($output);
}

to test the function above, let us use the jsontest.com's as the url

 print_r(json_decode(useCurl('http://ip.jsontest.com/ '), true));

that should output something like this

Array ( [0] => Array ( [result] => success [success] => 1.7.2 [source] => version [tag] => sampleTag [is_success] => 1 ) ) Array ( [ip] => 2605:e000:1213:40e3:c490:f413:c3f9:c617 ) 
0

It works with your jsontest.com URL but when I use my URL it returns nothing.

My hosts say they are not blocking the connections.

Any suggestions what to try now? (...he says pulling his hair out having wasted 7hours on this simple task already!)

This is the exact code I just tested, including the URL to the JASON I am trying to retrieve.

function useCurl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return($output);
    unset($output);
    }

print_r(json_decode(useCurl('http://82.145.61.242:25565/api/2/call?json=%5B%7B%22name%22%3A%22server.bukkit.version%22%2C%22key%22%3A%22a7584a0f6f1aa22d843ce0d46f88a2101ddae9285127f103092dee4904f9be35%22%2C%22username%22%3A%22ZonkedCompanion%22%2C%22arguments%22%3A%5B%5D%2C%22tag%22%3A%22sampleTag%22%7D%5D%22 '), true));

Edited by NuGG: added my current code

0

I have just tried it on another webserver and I can confim I get the same result - nothing gets printed. So doesnt look like a firewall issue on my webhost...

0

Try to add json_last_error() right after json_decode():

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
}

Source: http://www.php.net//manual/en/function.json-last-error.php

0

Tried it with json_last_error() as above and I still just get a blank page, no errors whatso ever, doesnt even echo " - No errors"

I was speaking to the host and they say port 25565 is closed on my webserver and this may be why.

When json_decode() tries to grab it, is it trying to use 25565 as the outgoing port from my webserver? I thought it would be using port 80?... Anyone know if this is the case? if it does use port 25565 to request the json then this must be why its failing?

0

this one works for me using your url and the cURL function above

$url = urldecode('http://82.145.61.242:25565/api/2/call?json=%5B%7B%22name%22%3A%22server.bukkit.version%22%2C%22key%22%3A%22a7584a0f6f1aa22d843ce0d46f88a2101ddae9285127f103092dee4904f9be35%22%2C%22username%22%3A%22ZonkedCompanion%22%2C%22arguments%22%3A%5B%5D%2C%22tag%22%3A%22sampleTag%22%7D%5D%22 ');

print_r(json_decode(useCurl($url), true));

the above returns

Array ( [0] => Array ( [result] => success [success] => 1.7.2 [source] => version [tag] => sampleTag [is_success] => 1 ) ) Array ( [0] => Array ( [result] => success [success] => 1.7.2-R0.3 [source] => server.bukkit.version [tag] => sampleTag [is_success] => 1 ) ) 

Just a side note:

(...he says pulling his hair out having wasted 7hours on this simple task already!)

If I will be pulling my hair for a simple task like this, I will be looking for a new job. Something that will inspire me on a daily basis and not stress me to death. Just my thoughts.

Edited by veedeoo: info added.

0

As I have previously stated the code returns nothing on my webserver, I have also tried it on a second server with the same result. (hence why im pulling my hair out)

<?

function useCurl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return($output);
    unset($output);
    }

$url = urldecode('http://82.145.61.242:25565/api/2/call?json=%5B%7B%22name%22%3A%22server.bukkit.version%22%2C%22key%22%3A%22a7584a0f6f1aa22d843ce0d46f88a2101ddae9285127f103092dee4904f9be35%22%2C%22username%22%3A%22ZonkedCompanion%22%2C%22arguments%22%3A%5B%5D%2C%22tag%22%3A%22sampleTag%22%7D%5D%22 ');
print_r(json_decode(useCurl($url), true));

?>

When my server requests the JSON does it do so from port 80 to port 25565 on the remote host? or from port 25565 on my webserver to port 25565 on the remote host?...

Port 25565 is blocked on my webserver.

0

try adding this just below the curl initialization

curl_setopt($ch, CURLOPT_PORT, '25565');

to test if the cURL is using the port 25565, try running this

 print_r(useCurl('http://portquiz.net/'));

If you need to know, what was the original port the cURL was using on outbound connection, remove

 curl_setopt($ch, CURLOPT_PORT, '25565');

and run this

 print_r(useCurl('http://portquiz.net/'));

Edited by veedeoo: info added.

0

This also returns nothing...

function useCurl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_PORT, '25565');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return($output);
    unset($output);
    }


print_r(useCurl('http://portquiz.net/'));

Not even an error.

0

the script above should return something like this on the browser

ba5cdc00487a0f6a2438956c7767298e

let us try this. Since there is not error showing up on your side, we create one for the sake of finding what is the situtation of the PHP parser and just maybe php.ini file.

create a new page and name it tester.php. Paste the following codes

<?php


    echo $this_error;

    print_r(error_get_last());

?>

direct your browser to this page and see if there is something on the page. The purpose of the test is to provide an undefined variable to purposely create an error for the error_get_last() function to catch.

Edited by veedeoo: info added.

0

OK, this...

 echo $this_error;
print_r(error_get_last());

Returns...

Array ( [type] => 8 [message] => Undefined variable: this_error [file] => /home/dsmwebne/public_html/_clients/_sites/_zonked/jsontest2.php [line] => 3 )

...and phpinfo() is a huge report, what specifically do you want to check? Errors are on, thats for sure.

0

I have just tried a third webhost with the exact code that veedeoo said works on his host... Still no joy.

So thats 3 webservers (one is a dedicated box) that I have tested this on and they all do the exact same thing - blank page, no errors.

What kind of webhost are you using veedeoo? How does it differ from the linux boxes im testing on?

0

@NuGG,

I have three different distros running on my virtual box. Centos, Ubuntu 64, and Red Hat. I never allowed my webhost to pre-load my server, because I want to prep my server for the application I am trying to test. I also have both the apache and Nginx.

0

Well I have a Redhat 64bit virtual box running Apache/2.2.15 which I installed, and that has virtually no restrictions on it with regards to filtering/firewalls and I get the exact same result on that as I do with the other two webhosts running cpanel.

This is very fustrating. I am now trying to test the script locally to the minecraft server and see if it can even fetch the json locally. (...but first I need to ask nicely for them to give me access!)

Can I confirm that this is the exact script you tested that worked?...

<?
function useCurl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, '25565');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return($output);
unset($output);
}
print_r(useCurl('http://82.145.61.242:25565/api/2/call?json=%5B%7B%22name%22%3A%22server.bukkit.version%22%2C%22key%22%3A%22a7584a0f6f1aa22d843ce0d46f88a2101ddae9285127f103092dee4904f9be35%22%2C%22username%22%3A%22ZonkedCompanion%22%2C%22arguments%22%3A%5B%5D%2C%22tag%22%3A%22sampleTag%22%7D%5D%22'));
?>

Thanks for your help on this, I really do appricate it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.