Suporte » Desenvolvendo com WordPress » Ajuda com php (iniciante)

  • ricardocnn

    (@ricardocnn)


    Olá a todos! Estou usando essa função no functions.php pra pegar o número de seguidores no Twitter :

    function getTwitterFollowers($screenName = ‘wpbeginner’)
    {
    // some variables
    $consumerKey = ‘YOUR_CONSUMER_KEY’;
    $consumerSecret = ‘YOUR_CONSUMER_SECRET’;
    $token = get_option(‘cfTwitterToken’);

    // get follower count from cache
    $numberOfFollowers = get_transient(‘cfTwitterFollowers’);

    // cache version does not exist or expired
    if (false === $numberOfFollowers) {
    // getting new auth bearer only if we don’t have one
    if(!$token) {
    // preparing credentials
    $credentials = $consumerKey . ‘:’ . $consumerSecret;
    $toSend = base64_encode($credentials);

    // http post arguments
    $args = array(
    ‘method’ => ‘POST’,
    ‘httpversion’ => ‘1.1’,
    ‘blocking’ => true,
    ‘headers’ => array(
    ‘Authorization’ => ‘Basic ‘ . $toSend,
    ‘Content-Type’ => ‘application/x-www-form-urlencoded;charset=UTF-8’
    ),
    ‘body’ => array( ‘grant_type’ => ‘client_credentials’ )
    );

    add_filter(‘https_ssl_verify’, ‘__return_false’);
    $response = wp_remote_post(‘https://api.twitter.com/oauth2/token’, $args);

    $keys = json_decode(wp_remote_retrieve_body($response));

    if($keys) {
    // saving token to wp_options table
    update_option(‘cfTwitterToken’, $keys->access_token);
    $token = $keys->access_token;
    }
    }
    // we have bearer token wether we obtained it from API or from options
    $args = array(
    ‘httpversion’ => ‘1.1’,
    ‘blocking’ => true,
    ‘headers’ => array(
    ‘Authorization’ => “Bearer $token”
    )
    );

    add_filter(‘https_ssl_verify’, ‘__return_false’);
    $api_url = “https://api.twitter.com/1.1/users/show.json?screen_name=$screenName”;
    $response = wp_remote_get($api_url, $args);

    if (!is_wp_error($response)) {
    $followers = json_decode(wp_remote_retrieve_body($response));
    $numberOfFollowers = $followers->followers_count;
    } else {
    // get old value and break
    $numberOfFollowers = get_option(‘cfNumberOfFollowers’);
    // uncomment below to debug
    //die($response->get_error_message());
    }

    // cache for an hour
    set_transient(‘cfTwitterFollowers’, $numberOfFollowers, 1*60*60);
    update_option(‘cfNumberOfFollowers’, $numberOfFollowers);
    }

    return $numberOfFollowers;
    }

    Aqui eu uso no single post pra mostar os seguidores :

    <div class=”entry-social”>
    <div class=”twitter”>
    <?php echo getTwitterFollowers(‘redes_sociais’); ?> Seguidores
    </div>
    </div>

    Duas perguntas
    1. ele mostra tudo certinho mas por que o link para a página do Twitter não funciona?

    2.o número aparece 43345. Como formato pra aparecer 43.345?

    Muito obrigado desde já!

Visualizando 2 respostas - 1 até 2 (de um total de 2)
  • Criador do tópico ricardocnn

    (@ricardocnn)

    
    <div class="entry-social">
    <div class="twitter">
    <a href=""https://www.twitter.com/redes_sociais";target="_blank"><?php echo getTwitterFollowers('redes_sociais'); ?> Seguidores</a>
    </div>
    </div>
    Criador do tópico ricardocnn

    (@ricardocnn)

    Não tava indo o jeito que eu coloco o link, enfim, qual é o jeito certo?

Visualizando 2 respostas - 1 até 2 (de um total de 2)
  • O tópico ‘Ajuda com php (iniciante)’ está fechado para novas respostas.