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

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.


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