Help with logic, using Twitter Rest API

1

Well, I have authentication ok with the code below:

<?php
if (isset($_POST['ttrSignin'])) {
  $ttrUsername = trim(filter_input(INPUT_POST, 'ttrUsername'));
  $ttrPassword = trim(filter_input(INPUT_POST, 'ttrPassword'));

  if (empty($ttrUsername)) {
    $error[] = 'Insira seu usuário';
  } elseif (empty($ttrPassword)) {
    $error[] = 'Insira sua senha';
  } elseif (!preg_match('/^[a-zA-Z0-9]+/', $ttrUsername)) {
    $error[] = 'Caracteres especiais detectados, se tiver usando <strong>@</strong>, remova-o.';
  } else {
    $ch = curl_init();

    $sTarget = "https://twitter.com";

    curl_setopt($ch, CURLOPT_URL, $sTarget);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_COOKIEFILE, ROOT . 'app' . SEPARATOR . 'cookies' . SEPARATOR . $ttrUsername . '_tweet.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, ROOT . 'app' . SEPARATOR . 'cookies' . SEPARATOR . $ttrUsername .'_tweet.txt');
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_REFERER, $sTarget);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);

    $html = curl_exec($ch);

    if(curl_errno($ch)) {
       echo 'error:' . curl_error($c);
    }

    preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $match);

    $authenticity_token = $match[1];

    if ($authenticity_token == ""); {       
      preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);   
      $authenticity_token = $matchprima[1];
    }


    $username = $ttrUsername;
    $password = $ttrPassword;

    $sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";

    $sTarget = "https://twitter.com/sessions";  

    curl_setopt($ch, CURLOPT_URL, $sTarget);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    # display server response
    $htmldos = curl_exec($ch);

    preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $htmldos, $matches);

    $cookies = array();

    foreach($matches[1] as $item) {
        parse_str($item, $cookie);
        $cookies = array_merge($cookies, $cookie);
    }

    if (empty($cookies['auth_token'])) {
      $error[] = 'Não foi possível conexão com o Twitter, Usuário e/ou Senha incorretos.';
    } else {
      Cookies::write('fm',                    $cookies['fm']);
      Cookies::write('kdt',                   $cookies['kdt']);
      Cookies::write('_twitter_sess',         $cookies['_twitter_sess']);
      Cookies::write('remember_checked_on',   $cookies['remember_checked_on']);
      Cookies::write('twid',                  $cookies['twid']);
      Cookies::write('auth_token',            $cookies['auth_token']);
      Cookies::write('lang',                  $cookies['lang']);
      Cookies::write('user',                  $ttrUsername);

      $_SESSION['twitter_session'] = $ttrUsername;
      header('Refresh: 2;' . URL_BASE . '/follow/?welcome=true');
      $success[] = 'Conexão bem sucedida, estamos te redirecionando.';

      // var_dump(Cookies::read('fm'));
      // var_dump(Cookies::read('kdt'));
      // var_dump(Cookies::read('_twitter_sess'));
      // var_dump(Cookies::read('remember_checked_on'));
      // var_dump(Cookies::read('twid'));
      // var_dump(Cookies::read('auth_token'));
      // var_dump(Cookies::read('lang'));
      // var_dump(Cookies::read('user'));

    }

    //var_dump($cookies);

    if(curl_errno($ch)) {
       echo 'error:' . curl_error($ch);
    }
  }
}
?>

I also have an alternate token generator:

<?php
$user_cookie = Cookies::read('user');

$key = CONSUMER_KEY;
$secret = CONSUMER_SECRET;
$api_endpoint = 'https://api.twitter.com/1.1/users/show.json?screen_name=' . $user_cookie;

// 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 = json_decode(curl_exec($br));

  curl_close($br);

  // do_something_here_with($data);
}
?>

And now I need to create friendships / create

So far I've been trying:

<?php

if (isset($_POST['ttrFollow'])) {
  $ttrUsername = trim(filter_input(INPUT_POST, 'ttrUsername'));

  if (empty($ttrUsername)) {
    $error[] = 'Insira seu nome de usuário.';
  } elseif (!preg_match('/^[a-zA-Z0-9]+/', $ttrUsername)) {
    $error[] = 'Caracteres especiais detectados, se tiver usando <strong>@</strong>, remova-o.';
  } elseif ($ttrUsername <> $user_cookie) {
    $error[] = 'Você não insereiu o seu nome de usuário!';
  } else {

    // $url = 'https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true';
    $url = 'https://api.twitter.com/1.1/friendships/create.json';

    $fr = curl_init();
    curl_setopt($fr, CURLOPT_URL,$url);
    curl_setopt($fr, CURLOPT_CAINFO, ROOT . 'app' . SEPARATOR . 'cacert' . SEPARATOR .'cacert-2017-06-07.pem');
    curl_setopt($fr, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($fr, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($fr, CURLOPT_RETURNTRANSFER,1);
    $buffer = curl_exec($fr);

    var_dump($buffer);

    curl_close($fr);
  }
}
?>

I do not know if error returns, but surely returns Bad Authentication , but that's not the case now.

As you can see, I created cookies files, using cURL , which logic I wanted:

Generate followers using these cookies files:

For example: If there is cookies inside the folder, and clicking the button win followers, these cookies come into action, every 10 followers win, I follow 5, could anyone help me with logic?

    
asked by anonymous 29.06.2017 / 22:50

1 answer

1

A concrete answer:

To use the API, you need to use OAuth, you can not use it any other way. This code will return error 215 .

This is the answer. Give it up, unless someone else has already done it.

    
07.07.2017 / 03:12