Tenho o seguinte código feito com javascript
:
<script type="text/javascript">
$(function() {
$("#loginForm").on("submit", function(a) {
a.preventDefault(), $("#signinButton").attr("value", "Autenticando...");
var user = $("#username").val();
var pass = $("#password").val();
if (user == "") {
$("#error").css("display", "block"), $('#error').html("<i class='fa fa-warning'></i> Insira seu usuário do Twitter"), $("#signinButton").attr("value", "Entrar");
}
else if (pass == "") {
$("#error").css("display", "block"), $('#error').html("<i class='fa fa-warning'></i> Insira sua senha do Twitter"), $("#signinButton").attr("value", "Entrar");
} else {
$("#error").hide();
}
var b = $("#username").val();
0 == /^[a-zA-Z0-9_ ]*$/.test(b) ? ($("#error").css("display", "block"), $("#error").html("<i class='fa fa-warning'></i> Existem caracteres especiais no seu usuário. Se estiver usando <strong>@</strong> remova-o!"), $("#signinButton").attr("value", "Entrar")) : $.ajax({
type: "POST",
url: "api/login.php",
dataType: "JSON",
data: $("#loginForm").serialize(),
success: function(a) {
1 == a.redirect ? window.location = "index.php" : ($("#error").css("display", "block"), $("#error").html(a.message)), $("#signinButton").attr("value", "Entrar");
}
})
})
})
</script>
Ta funcionando certinho, verificando campos vazios, se existe caracteres especais... Se preenchido corretamente fica Autenticando... e não continua meu script, repare que estou usando URL: "api/login.php"
e dataType: "JSON",
Este é o arquivo em api/login.php
:
<?php
require_once '../modules/autoload.php';
$ttrUsername = trim(filter_input(INPUT_POST, 'username'));
$ttrPassword = trim(filter_input(INPUT_POST, 'password'));
$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 . 'api' . SEPARATOR . 'cookies' . SEPARATOR . $ttrUsername . '.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, ROOT . 'api' . SEPARATOR . 'cookies' . SEPARATOR . $ttrUsername . '.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(curl_errno($ch)) {
echo 'error:' . curl_error($ch);
}
return json_decode($htmldos);
O que está errado neste código? Tanto o JavaScript quanto o PHP, eu estou retornando um json
em api/login.php
, mas não está funcionando.
json_decode
? Não seriajson_encode
? E aparece algum erro no console? Já testou utilizar a callback de erro do AJAX no jQuery para saber se deu erro na requisição?json_encode
continua na mesmasuccess
:error: function (error) { console.log(error); }