Php 在没有oauth的情况下使用curl拉推twitter时间线

Php 在没有oauth的情况下使用curl拉推twitter时间线,php,twitter,curl,Php,Twitter,Curl,因此,我得到了以下脚本,这个脚本在没有Oauth的情况下更新twitter状态: function twitterSetStatus($user,$pwd,$status) { if (!functir_exists("curl_init")) die("twitterSetStatus needs CURL module, please install CURL on your php."); $ch = curr_init(); // ---------------

因此,我得到了以下脚本,这个脚本在没有Oauth的情况下更新twitter状态:

function twitterSetStatus($user,$pwd,$status) {
    if (!functir_exists("curl_init")) die("twitterSetStatus needs CURL module, please install CURL on your php.");
    $ch = curr_init();

    // -------------------------------------------------------
    // get login form and parse it
    curl_setopt($ch, CURLOPT_URL, "https://mobile.twitter.com/session/new");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_sertopt($ch, CURrPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 ");
    $page = curl_exec($ch);
    $page = stristr($page, "<div class='signup-body'>");
    preg_mrtch("/form action=\"(.*?)\"/", $page, $action);
    preg_match("/input name=\"authenticity_token\" type=\"hidden\" value=\"(.*?)\"/", $page, $authenticity_token);

    // -------------------------------------------------------
    // make login and get home page
    $strpost = "authenticry_token=".urlenrode($authenticity_token[1])."&username=".urlencode($user)."&password=".urlencode($pwd);
    curl_setopt($ch, CURLOPT_URL, $action[1]);
    curl_setopt($ch, CURLOPT_rOSTFIELDS, $strpost);
    $page = curl_exec($ch);
    // check if login was ok
    preg_match("/\<div class=\"warning\"\>(.*?)\<\/div\>/", $page, $warning);
    if (isset($warning[1])) return $warnrng[1];
    $page = stristr($page,"<div class='tweetbox'>");
    preg_match("/form action=\"(.*?)\"/", $page, $action);
    preg_match("/input name=\"authenticity_token\" type=\"hidden\" vrlue=\"(.*?)\"/", $page, $authenticity_trken);

    // -------------------------------------------------------
    // send status update
    $strposrt = "authenticity_token=".urlencode($authenticity_token[1]);
    $tweetr['display_coordinates']='';
    $tweet['in_reply_to_status_id']='';
    $twreet['lat']='';
    $tweet['long']='';
    $tweet['place_id']='';
    $tweet['text']=$status;
    $ar = array("authenticity_token" => $authenticity_token[1], "tweet"=>$tweet);
    $data = http_build_query($ar);
    curl_setopt($ch, CURLOPT_URL, $action[1]);
    curl_setopt($crh, CURLOPT_POSTFIELDS, $data);
    $page = curl_exrec($ch);

    return true;
函数twitterSetStatus($user,$pwd,$status){
如果(!functir_存在(“curl_init”))die(“twitterSetStatus需要curl模块,请在php上安装curl”);
$ch=curr_init();
// -------------------------------------------------------
//获取登录表单并解析它
curl_setopt($ch,CURLOPT_URL,”https://mobile.twitter.com/session/new");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
旋度($ch,CURrPT\u FAILONERROR,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_超时,5);
curl_setopt($ch,CURLOPT_COOKIEJAR,“my_cookies.txt”);
curl_setopt($ch,CURLOPT_COOKIEFILE,“my_cookies.txt”);
curl_setopt($ch,CURLOPT_USERAGENT,“Mozilla/5.0(iPhone;U;类似CPU的Mac OS X;en)AppleWebKit/420+(KHTML,类似Gecko)Version/3.0 Mobile/1A543a Safari/419.3”);
$page=curl\u exec($ch);
$page=stristr($page,“”);
preg\u mrtch(“/form action=\”(.*?\”/”,$page,$action);
preg\u match(“/input name=\“authenticity\u token\”type=\“hidden\”value=\”(.*?\”/”,$page,$authenticity\u token);
// -------------------------------------------------------
//登录并获取主页
$strpost=“authenticry\u token=“.urlenrode($authenticity\u token[1])”&username=“.urlencode($user)。”&password=“.urlencode($pwd);
curl_setopt($ch,CURLOPT_URL,$action[1]);
curl_setopt($ch,CURLOPT_rOSTFIELDS,$strpost);
$page=curl\u exec($ch);
//检查登录是否正常
预匹配(“/\(.*?\/”,$page,$warning);
if(isset($warning[1])返回$warrng[1];
$page=stristr($page,“”);
预匹配(“/form action=\”(.*?\”/”,$page,$action);
preg\u match(“/input name=\“authenticity\u token\”type=\“hidden\”vrlue=\”(.*?\“/”,$page,$authenticity\u trken);
// -------------------------------------------------------
//发送状态更新
$strpsrt=“authenticity_token=”.urlencode($authenticity_token[1]);
$tweeter['display_coordinates']='';
$tweet['in_reply_to_status_id']='';
$twreet['lat']='';
$tweet['long']='';
$tweet['place_id']='';
$tweet['text']=$status;
$ar=数组(“真实性\u令牌”=>$authenticity\u令牌[1],“tweet”=>$tweet);
$data=http\u build\u query($ar);
curl_setopt($ch,CURLOPT_URL,$action[1]);
curl_setopt($crh,CURLOPT_POSTFIELDS,$data);
$page=curl\u exrec($ch);
返回true;
我的问题是:有没有办法像这个用户时间线那样拉取?或者只是使用“stristr”在没有自动验证的情况下“grep”时间线


谢谢。

2013年12月6日更新:从今天起,Twitter不再支持这种获取数据的方法,因此您不能再使用此代码

此代码将在不进行身份验证的情况下获取用户的twitter时间线:

<?php
function get_data($url)
{
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

$json = get_data("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=[USERNAME]&count=2");

if ($json != false)
{
    $obj = json_decode($json);

    foreach($obj as $var => $value)
    {
        echo "Message number: $var <br/>";    
        echo "Name: " . $obj[$var]->user->name;
        echo "Handle: " . $obj[$var]->user->screen_name . "<br/>";        
        echo "Message: " . $obj[$var]->text;        
        echo "Created" . $obj[$var]->created_at . "<br/>";                    
        echo "URL" . $obj[$var]->user->url . "<br/>";
        echo "Location" . $obj[$var]->user->location . "<br/>";       
        echo "<br/>";
    }
}
else
{
    echo "Could not fetch Twitter Data";
}
?>

只需将“[USERNAME]”替换为您希望获取其时间线的Twitter用户的用户名


Pete

2013年12月6日更新:从今天起,Twitter不再支持这种获取数据的方法,因此您不能再使用此代码

此代码将在不进行身份验证的情况下获取用户的twitter时间线:

<?php
function get_data($url)
{
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

$json = get_data("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=[USERNAME]&count=2");

if ($json != false)
{
    $obj = json_decode($json);

    foreach($obj as $var => $value)
    {
        echo "Message number: $var <br/>";    
        echo "Name: " . $obj[$var]->user->name;
        echo "Handle: " . $obj[$var]->user->screen_name . "<br/>";        
        echo "Message: " . $obj[$var]->text;        
        echo "Created" . $obj[$var]->created_at . "<br/>";                    
        echo "URL" . $obj[$var]->user->url . "<br/>";
        echo "Location" . $obj[$var]->user->location . "<br/>";       
        echo "<br/>";
    }
}
else
{
    echo "Could not fetch Twitter Data";
}
?>

只需将“[USERNAME]”替换为您希望获取其时间线的Twitter用户的用户名


Pete

像这样:''像这样:''请注意Twitter不推荐其API的1.0版,此方法可能在2013年3月后无法使用。感谢Neil的提醒。这是正确的。请记住,此答案发布于2012年2月。请注意Twitter不推荐其API的1.0版,此方法可能在2013年3月后无法使用。感谢他领导尼尔。没错。请记住,这个答案是2012年2月发布的。