Twitter API with CURL

Asked

Viewed 136 times

1

OBS: THE COMPLETE CODE IS HERE, but I made this adjustment to receive the cookies

I am trying to read the cookie files to make the necessary requests to stop get the result I hope, see my code:

$tokens = ['_twitter_sess', 'ct0', 'auth_token'];

        foreach (glob($this->_cookieFile) as $file) {
            $pointer = fopen($file, 'r');
        while (!feof($pointer)) {
        $line = fgets($pointer);

        foreach ($tokens as $token) {
          if (stripos($line, $token) !== false) {
            var_dump($line);
            curl_setopt_array($request, [
                                CURLOPT_COOKIEFILE  => $line,
                                CURLOPT_HTTPHEADER          => [
                          'origin: https://twitter.com',
                          'authorization: Bearer ' . $bearer,
                          'x-csrf-token: ' . $line,
                          'referer: https://twitter.com/',
                          'x-twitter-auth-type: OAuth2Session',
                          'x-twitter-active-user: yes',
                        ],
                         CURLOPT_POSTFIELDS => http_build_query([
                              'challenges_passed'   => false,
                              'handles_challenges'  => 1,
                              'include_blocked_by'  => true,
                              'include_blocking'    => true,
                              'include_can_dm'      => true,
                              'include_followed_by' => true,
                              'include_mute_edge'   => true,
                              'skip_status'         => true,
                              'user_id'             => Session::get('username'),
                                ], '', '&', PHP_QUERY_RFC3986),
                            ]
                        );
            break;
          }
        }
        }
        }

But when giving a var_dump in $line, I receive as return:

inserir a descrição da imagem aqui

I need to read each line in one array associativo or read all cookies at once.

So how do I return from mine cURL get back to me:

'{"errors":[{"code":220,"message":"Your credentials do not allow access to this resource."}]}' (length=92)
  • What associative array would that be?

  • Example: $line['ct0'], $line['_twitter_sess'] ... understands?

  • 1

    This is a format Netscape, this link has all the documentation. Also if you are picking up the cookie you do not need to use this format, you have the CURLOPT_HEADERFUNCTION which allows you to extract this directly from the header. Also you don’t need any associative array, at least Curl already supports the Netscape format if you use the "cookie engine" (i.e COOKIEFILE or COOKIELIST), me said it here.

  • @Inkeliz, but if I’m saved the file, what’s the logic in not using it? The yes I saw this answer. I want to use the file, as you said in the answer, CUROPT_COOKIE would solve right ? but I have 7 cookies saved in the format Netscape. I need everyone.

  • 3

    Possible duplicate of Turning archive content into cookies

  • @Andersoncarloswoss Duplicate, but where’s the answer?... I’ll erase that one and leave this one.

  • 2

    Duplicate in the sense that you’re polluting the community with repeated questions because you didn’t get the answer. This is not good practice and should not be encouraged. If you have asked the question before and no one has answered, it may be interesting to review the question by observing what can improve or offer a reward.

  • @Andersoncarloswoss, you’re right, forgive me, but my question is completely different now. Ta tense solve this problem.

  • 1

    If it’s already on file, use the CURLOPT_COOKIEFILE it accepts this format normally. See the documentation, it accepts both in Netscape format and in "Set-Cookie format".

  • @Inkeliz, but does it read all the files from the folder? I need to put in case I just need the twitter_sess and auth_token, since ct0 saved in a bank.

Show 5 more comments

1 answer

1


If you really want an array, which by me doesn’t make any sense and seems to me to be one that this issue is an "XY problem". But anyway, just "explode" for each \t.

So, supposing it’s in string:

$cookie = '# Netscape HTTP Cookie File

.example.org    TRUE    /   FALSE   1507286655  remember_me true
.example.org    TRUE    /   FALSE   1507286655  APISID  DijdSAOAjgwijnhFMndsjiejfdSDNSgfsikasASIfgijsowITITeoknsd
.example.org    TRUE    /   FALSE   1507286655  static_files    iy1aBf1JhQR';


$array = [];

foreach(array_slice(explode("\n", $cookie), 2) as $linha){

    $colunas = explode("\t", $linha);
    $array[$colunas[5]] = $colunas[6];

}

Upshot (var_dump($array)):

array(3) { ["remember_me"]=> string(5) "true " ["APISID"]=> string(58) "DijdSAOAjgwijnhFMndsjiejfdSDNSgfsikasASIfgijsowITITeoknsd " ["static_files"]=> string(11) "iy1aBf1JhQR" }

The idea is simple, ignore the first two lines (array_slice(..., 2)) then for each line break by tab (\n) and then get the name and value information.


'Cause I think that’s totally wrong:

  1. You have native features for this on Curl, all features that use Curl’s cookie management engine handle this Netscape format well.
  2. You have how to use the CURLOPT_HEADERFUNCTION to filter cookies, if you are making a request to save such cookies.
  3. You can also use the CURLINFO_COOKIELIST to obtain the same information, filter and save it, leaving only the data that matters.
  4. You’re storing unnecessary data, worsening performance by having to filter it through every run, with no benefit.
  • 1

    Well I saw your answer HERE, I understood the concept, I will review my cases, but I have several files in Netscape @Inkeliz format, I need to use them all. I think you got what I want to do. I’ll mark it as solved since your answer answers my question.

  • 1

    @Inkeliz, his doubt was the same as my OAKSOKOAKS I tbm am saving all cookies in separate files as you can see [here] (http://prntscr.com/ghrc8u) but there is one however, both the CURLOPT_COOKIEFILE as to the COOKIELIST won’t list all correct? , you said you have the HEADERFUNCTION but I pick up these cookies on login, saved in TXT to then make the request in the API, I believe he does not need all this, Now we are two in doubt, how to read all these COOKIES without this gambiarra there ? The.o

  • 2

    The HEADERFUNCTION allows you to create a header reading function, you are free to create as you want the filter you want and store as you want. In case, you can only take the cookies you want (example, a, b, c and ignore the z, y). With a little performance in mind you would store just like a=valor;b=valor2;c=valor3;, could store in the database in a table and then just use CURLOPT_COOKIE it will already be formatted correctly. COOKIELIST lists all in Netscape format (the same as OP), so it would filter and save (or instead of always saving and filtering).

  • 1

    Show, in case instead of saving the saved file directly in the bank? and then use CURLOPT_COOKIE to recover these cookies? Well I will save in files and bank to see what happens, I liked the answer.

  • COOKIE_FILE The file name containing the cookie data. The cookie file may be in Netscape format, or simply discarded HTTP-style headers in a file. If the name is an empty string, no cookie will be loaded, but cookie management is still enabled. So my logic is right, if it is in files with different names I need this array with a loop.

  • What doesn’t make sense is you having garbage in the file. If you want can also erase the unnecessary lines from the file only once, the correct one would be not even store the unnecessary information. You can even create a flag # X or a column in the database, if there is such information indicates that the data are clean and pass directly to the COOKIEFILE. That’s the question, you asked a question thinking that this was the only solution, but it doesn’t explain the need for this, the problem of fact, running this "filter" all the time is an unnecessary expense

Show 1 more comment

Browser other questions tagged

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