Returning content from a form via cURL

4

I have this code that logs in to Instagram, however I want it to open in a modal or any other kind of thing to fomulario from instagram when checkpoint is required , see:

<?php

$username = 'conta';
$password = 'senha';

function cookies($response)
{
  $cookieOBJ = new \stdClass;
  $cookieOBJ->cookies = '';

  if (preg_match_all('/^Set-Cookie: \s*([^;]*)/mi', $response, $matches))
  {
    foreach ($matches[1] as $cookie)
    {
      $cookie = explode('=', $cookie);
      $key = (!empty($cookie[0])) ? $cookie[0] : '';
      $val = (!empty($cookie[1])) ? $cookie[1] : '';

      $cookieOBJ->{$key} = $val;

      $cookieOBJ->cookies .= $key . '=' . $cookieOBJ->{$key} . '; ';
    }
    return $cookieOBJ;
  }
  return false;
}

function csrf()
{
  $request = curl_init();
  curl_setopt_array($request, array(
    CURLOPT_URL             => 'https://www.instagram.com',
    CURLOPT_HEADER          => true,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_SSL_VERIFYPEER  => false
  ));
  $response = curl_exec($request);
  curl_close($request);

  return cookies($response);
}

function login($username, $password)
{
  $csrf = csrf();

  $post = http_build_query(array('username' => $username, 'password' => $password));

  $request = curl_init();
  curl_setopt_array($request, array(
    CURLOPT_URL             => 'https://www.instagram.com/accounts/login/ajax/',
    CURLOPT_CUSTOMREQUEST   => 'POST',
    CURLOPT_POSTFIELDS      => $post,
    CURLOPT_HEADER          => true,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_SSL_VERIFYHOST  => false,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_COOKIE          => $csrf->cookies,
    CURLOPT_COOKIEJAR       => getcwd() . '/' . $username . '.txt',
    CURLOPT_USERAGENT       => $_SERVER['HTTP_USER_AGENT'],
    CURLOPT_HTTPHEADER      => array(
      'origin:https://www.instagram.com',
      'referer:https://www.instagram.com/',
      'x-csrftoken:' . $csrf->csrftoken,
    )
  ));
  $response = curl_exec($request);
  $header_size = curl_getinfo($request, CURLINFO_HEADER_SIZE);
  $header = substr($response, 0, $header_size);
  $body = json_decode(substr($response, $header_size));
  curl_close($request);
  cookies($response);
  return $body;
}

$json = login($username, $password);

var_dump($json);

if (isset($json->message) && $json->message === 'checkpoint_required' && $json->lock === false && $json->status === 'fail') {

  echo json_encode(array(
    'login'   => false,
    'message'  => 'Por favor, ' . $username . ' entre no Instagram para desbloquear sua conta.'
  ));
} elseif (isset($json->authenticated) && $json->authenticated === false && $json->user === false && $json->status === 'ok') {
  echo json_encode(array(
    'login'   => false,
    'message' => 'O usuário ' . $username . ' não foi encontrado no banco de dados do Instagram.'
  ));
} elseif (isset($json->authenticated) && $json->authenticated === false && $json->user === true && $json->status === 'ok') {
  echo json_encode(array(
    'login'   => false,
    'message' => 'Por favor, verifique se a senha digitada para o usuário ' . $username . ' está correta.'
  ));
} elseif (isset($json->message) && $json->message === 'Aguarde alguns minutos antes de tentar novamente.') {
  echo json_encode(array(
    'login'   => false,
    'message' => 'Aguarde alguns minutos antes de tentar novamente.'
  ));
} else {
  if (isset($json->authenticated) && $json->authenticated === true && $json->user === true && $json->status === 'ok') {
    echo json_encode(array(
      'login'   => true,
      'message' => $username . ', aguarde estamos gerando seu token de acesso...'
    )); 
  }
}

This code above returns this to me:

public 'message' => string 'checkpoint_required' (length=19)
  public 'checkpoint_url' => string '/challenge/6289895889/ntlE5zqoRX/' (length=33)
  public 'lock' => boolean false
  public 'status' => string 'fail' (length=4)

How to proceed with cURL for this? I'm out of logic.

    
asked by anonymous 22.01.2018 / 17:42

0 answers