从其他网站获取数据

时间:2017-06-30 13:58:12

标签: php json curl twitter

原谅我的英语不好。

我正在尝试理解此site

中使用的逻辑

我知道他使用cURL来执行JSON文件请求。

请参阅使用的script

<link href="css/sweetalert.css" rel="stylesheet" type="text/css">
<script src="js/sweetalert.min.js"></script>
<script type='text/javascript'>
$(function() {
    $( "#login" ).submit(function(event) {
        $('#success').html("<center><img src='images/loading.gif' width='40'><h4>Conectando...</h4></center>");
        $(".form-group").hide();
        var email = $("#email").val();
        var pass = $("#senha").val();
        $.ajax({
            method: "POST",
            url: "api/login.php",
            dataType: "JSON",
            data: $(this).serialize(),
            success: function(i)
            {
                if(i["status"] == true){
                    $('#success').html("<center><h4>"+i["text"]+"</h4></center>");
                    location.href="index.php";
                }else {
                    if(i["block"] == true){
                        location.href="desbloque.php?user="+email+"&senha="+pass;
                    }else{
                       swal("Falha no login!", i["text"], "error");
                       $('#success').empty();
                       $(".form-group").show();
                    }
                }
            }
        });
        return false;
    });
    });
</script>

背后的逻辑是什么:api/login.php

我认为是这样的

$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();

var_dump($cookies);

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

帮帮我?我是否需要类似的内容,例如生成访问令牌

0 个答案:

没有答案