Turn CURL into class

Asked

Viewed 194 times

0

I would like to transform my code curl which is structural and puts it into a classe, but I’m out of logic, I DON’T WANT ANYTHING READY, just want examples, see my class.

<?php

$cookie = [];

$username = trim(filter_input(INPUT_POST, 'username'));
$password = trim(filter_input(INPUT_POST, 'password'));

$index_url = 'https://twitter.com';

$token = curl_init();
curl_setopt_array($token, [
      CURLOPT_URL             => $index_url,
      CURLOPT_CUSTOMREQUEST   => 'GET',
      CURLOPT_RETURNTRANSFER  => true,
      CURLOPT_SSL_VERIFYPEER  => false,
      CURLOPT_SSL_VERIFYHOST  => 2,
      CURLOPT_USERAGENT       => $_SERVER['HTTP_USER_AGENT'],
      CURLOPT_COOKIEJAR       => __DIR__ . SEPARATOR . 'cookies' . SEPARATOR . $username . '.txt',
      CURLOPT_COOKIESESSION   => true,
      CURLOPT_REFERER         => $index_url,
      CURLOPT_HEADER          => true,
      CURLOPT_HTTPHEADER      => ['Cookie:' . http_build_query($cookie, '', ';') . ';'],
      CURLOPT_HEADERFUNCTION => function ($curl, $header) use (&$cookie) {
        if (stripos($header, 'Set-Cookie:') === 0) {
          if (preg_match('/Set-Cookie:\s?(.*?)=(.*?);/i', $header, $matches)) {
            $cookie[$matches[1]] = urldecode($matches[2]);
          }
        }
        return strlen($header);
      }
    ]
);    
$access = curl_exec($token);

preg_match('/value="(.*?)" name="authenticity_token"/', $access, $matches);

$authenticity_token = $matches[1];

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

$session_url = 'https://twitter.com/sessions';

curl_setopt_array($token, [
      CURLOPT_URL             => $session_url,
      CURLOPT_CUSTOMREQUEST   => 'POST',
      CURLOPT_POSTFIELDS      => $session_post,
      CURLOPT_RETURNTRANSFER  => true,
      CURLOPT_HTTPHEADER      => [
        "Content-type: application/x-www-form-urlencoded",
        'Cookie: '. http_build_query($cookie, '', ';').';',
      ],
      CURLOPT_USERAGENT       => $_SERVER['HTTP_USER_AGENT'],
      CURLOPT_HEADER          => true,
      CURLOPT_FOLLOWLOCATION  => true,
      CURLOPT_MAXREDIRS       => 2,
      CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
      CURLOPT_POSTREDIR       => 2,
      CURLOPT_AUTOREFERER     => 1
  ]

);
$auth = curl_exec($token);

if (isset($cookie['auth_token']))
{
  $twid = filter_var($cookie['twid'], FILTER_SANITIZE_NUMBER_INT);

  Cookies::set('login_token', $cookie['ct0']);
  Cookies::set('kdt', $cookie['kdt']);
  Cookies::set('user_id', $twid);
  Cookies::set('auth_token', $cookie['auth_token']);
  Cookies::set('username', $username);

  $_SESSION[SITE_TITLE . '_session'] = $username;

  echo json_encode(array(
    "status"      => "success",
    "message"     => "Autenticação bem sucedida, estamos te redirecionando.",
  ));
}
else
{
  echo json_encode(
    array(
      "status" => "error",
      'message'=> "Não foi possível autenticar com o Twitter.",
    ));
}

I have no logic to turn this into Object Orientation, I wanted to put this exposed code upstairs within a class, so that I can reuse it in other processes in the future, but I’m no longer logical, a programmer friend told me on Facebook: I recommend creating a class for each CURL I’m using MVC and created a helper see:

<?php

class Curl {

}

It contains just that. I’m looking for examples on the internet, but I can’t find

I tried this as an example

But with me it did not work, nor shows errors, no wamp did not create error log. I wanted a basic example. The rest I made my jumps.

EDITED

I think this is enough I really am without logic, I need help :(

<?php

class Curl {

    public function setUsername($username) {

    }

    public function setPassword($password) {

    }

    public function curlInit($init) {

    }

    public function curlClose($close) {

    }
}
  • 1

    You will first need to analyze the situation and evaluate what attributes and methods you will need in the class, just like you do with any other class. A process that might make it easier is to do something similar to what happens with the Mysqli library, where there is always a method for each structural function.

  • 1

    I never messed with mysqli_* but I’ll see what I can do. I trust your comments because you’ve helped me a lot.

  • @Andersoncarloswoss If I follow the same example of PDO serves as well ? I already know the methods I will use. it’s going to be a mess but then I show it clean.

No answers

Browser other questions tagged

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