Extract data from an array

Asked

Viewed 190 times

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)); get back to 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...

  • 3

    $data is a text in JSON format, so use the function json_decode to convert to a PHP object.

1 answer

0

How is a JSON that you want to extract, just do it:

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

and then test

echo $data->screen_ name;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.