Extract data from an array

0

I have the following code:

<?php
require_once './app/autoload.php';

if (!isset($_SESSION['twitter_session'])) {
    header('Location:' . URL_BASE . '/index.php?access=denied');
}

if (isset($_POST['send'])) {
    $user = trim(filter_input(INPUT_POST, 'username'));


    if (empty($user)) {
        $error[] = 'Preencha o campo!';
    } elseif ($user <> $_SESSION['twitter_session']) {
        $error[] = 'Parece que você não está usando o seu Usuário!';
    } else {

        $key = 'oIzKM70lcDLG0ewPYLUk73sWH';
        $secret = 'uvolAqivNDk7tDwvYgaix0cHVTpY0teIAGp8TvDblQF8wXw82h';
        $api_endpoint = 'https://api.twitter.com/1.1/users/show.json?screen_name=' . $_SESSION['twitter_session']; // endpoint must support "Application-only authentication"

        // request token
        $basic_credentials = base64_encode($key.':'.$secret);
        $tk = curl_init('https://api.twitter.com/oauth2/token');
        curl_setopt($tk, CURLOPT_CAINFO, ROOT . 'app' . SEPARATOR . 'cacert' . SEPARATOR .'cacert-2017-06-07.pem');
        curl_setopt($tk, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($tk, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($tk, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$basic_credentials, 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
        curl_setopt($tk, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
        curl_setopt($tk, CURLOPT_RETURNTRANSFER, true);
        $token = json_decode(curl_exec($tk));

        curl_close($tk);

        // use token
        if (isset($token->token_type) && $token->token_type == 'bearer') {
            $br = curl_init($api_endpoint);
            curl_setopt($br, CURLOPT_CAINFO, ROOT . 'app' . SEPARATOR . 'cacert' . SEPARATOR .'cacert-2017-06-07.pem');
            curl_setopt($br, CURLOPT_SSL_VERIFYPEER, 1);
            curl_setopt($br, CURLOPT_SSL_VERIFYHOST, 2);
            curl_setopt($br, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token->access_token));
            curl_setopt($br, CURLOPT_RETURNTRANSFER, true);

            $data = curl_exec($br);

            curl_close($br);

            // do_something_here_with($data);
        }
    }
}
?>

When I give a var_dumb($data = curl_exec($br)); it returns me:

C:\wamp64\www\aprendiz\follow.php:46:string '{"id":866687457990979584,"id_str":"866687457990979584","name":"Sertanejo com letra","screen_name":"com_letra","location":"","profile_location":null,"description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":32,"friends_count":76,"listed_count":0,"created_at":"Mon May 22 16:09:27 +0000 2017","favourites_count":413,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":113,"lang":"pt","status":{"created_at":"Wed Jun 28 17:21:48 +000'... (length=3166)

How can I extract and use for example: echo $data->screen_name; ?

I've tried with foreach but I'm out of logic ...

    
asked by anonymous 29.06.2017 / 01:30

1 answer

1

Since it's a% w_that you want to extract, just do this:

JSON :

and then test

$data = json_decode(curl_exec($br));

    
29.06.2017 / 01:40