Jump to content
techker

Json parse from url

Recommended Posts

Hey guy's im calling an api and getting back 

 

{

    "user_info": {
        "username": "u",
        "password": "p",
        "message": "",
        "auth": 1,
        "status": "Active",
        "exp_date": "1521306691",
        "is_trial": "0",
        "active_cons": "0",
        "created_at": "1518887491",
        "max_connections": "1",
        "allowed_output_formats": [
            "m3u8",
            "ts",
            "rtmp"
        ]
    },
    "server_info": {
        "url": "server",
        "port": "25461",
        "rtmp_port": "8001",
        "timezone": "Europe/London",
        "time_now": "2018-03-06 14:05:59"
    }
}
 
im trying to parse in ph to get username and password.
 
I have tried lots of ways but no success?
 
example :
  $data = json_decode(file_get_contents($url), true);
        echo "id: ", $data['user_info']['username'];
        echo '<br>';
        echo "Name: ", $data['password'];
        echo '<br>';
       

or

$obj = json_decode($url2);
print $obj->{'username'};

or

$jfo = file_get_contents($json_file);
// read the title value
$title = $jfo->server_info->url;
// copy the posts array to a php var
$posts = $jfo->user_info->username;

no luck?do I have to use like a multilevel son method? 

 

Share this post


Link to post
Share on other sites

The first method should work. If it doesn't then $url is wrong or the returned JSON does not look like what you think it looks like.

Dump out $data to see what you're working with.

 

The second won't because json_decode does not accept URLs. It wants a JSON string. Someone didn't read the documentation.

The third won't because file_get_contents returns a string. It does not decode JSON. Someone didn't read the documentation.

Share this post


Link to post
Share on other sites

json_decode() is returning an object. Try:

 

 

$jsonStr = file_get_contents($url);
$dataObj = json_decode($jsonStr);
echo $dataObj->user_info->username;

Share this post


Link to post
Share on other sites

its odd cause im getting an error connection refused..

 

but when I use postman it get the son data

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.