Jump to content

Json parse from url


techker

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? 

 

Link to comment
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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.