百度空间 | 百度首页 
               
 
查看文章
 
省去m.twitter.com手机访问需要登录的php
2008-05-12 18:50
Twitter的手机访问界面m.twitter.com每次访问都需要输入密码,很不方便。看了一下Twitter API发文比较简单,原理为:

Post a status update, authenticated: curl -u email:password -d status="your message here" http://twitter.com/statuses/update.xml

因此就可利用curl写个脚本,把自己用户名、密码设进去以后就不用每次登录了。

<?php
header ('Content-type: text/html; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';

/*
POST Status update to Twitter USING PHP AND CURL
Author, Tim, iso1600 at gmail dot com
*/

// Your Twitter username/password
$my_username="username"; //change this to your login username
$my_password="password"; // change this to your login password

// What are you doing?
$status = $_POST['status'];
if ($status != '') {
$content = "status=".urlencode($status);
$headers = array( "Content-type: application/x-www-form-urlencoded" );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/update.xml");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $my_username.':'.$my_password);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);

$data = curl_exec($ch);

if (curl_errno($ch)) {
print curl_error($ch);
} else {
curl_close($ch);
}
}
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Twitter mobile post</title></head><body>
<form method="post" action="twitter.php"> <input type="text" name="status"> <input type="submit" > </form>
<?php

// Get your twitter homepage.
$myt = curl_init();
curl_setopt($myt, CURLOPT_URL, "http://m.twitter.com/".$my_username);
$html = curl_exec($myt);
$p1 = strpos($html, "<body ");
$p2 = strpos($html, ">", p1) + 1;
$p3 = strpos($html, "</body>");
echo substr($html, $p2, $p3);
?>
</body></html>


写完后传到某虚拟主机测试,发现调用失败,因为大部分虚拟主机不支持curl。
那只得改用socket, php4, 按最低配置写,经过PC和手机测试都可以成功更新状态。
有PHP虚拟主机的朋友可以上传到某个隐藏路径,方便自己用,改下用户和密码就行了。

<?php
header ('Content-type: text/html; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
/*
POST Status update to Twitter USING PHP4 AND socket
Author, Tim, iso1600 at gmail dot com
*/

// Your Twitter username/password
$my_username="username"; //change this to your login username
$my_password="password"; // change this to your login password

// What are you doing?
$status = $_POST['status'];
if ($status != '') {
$content = "status=".urlencode($status);
$headers = "Authorization: Basic " . base64_encode("$my_username:$my_password");
do_post_request("http://twitter.com/statuses/update.xml", $content, $headers);
}
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Twitter mobile post</title></head><body>
<form method="post" action="twitter.php"> <input type="text" name="status"> <input type="submit" > </form>
<?php

// Get your twitter homepage
$html = do_get_request("http://m.twitter.com/".$my_username);
$p1 = strpos($html, "<body ");
$p2 = strpos($html, ">", p1) + 1;
$p3 = strpos($html, "</body>");
echo substr($html, $p2, $p3);

function do_post_request($url, $data, $optional_headers = null) {
$start = strpos($url,'//')+2;
$end = strpos($url,'/',$start);
$host = substr($url, $start, $end-$start);
$domain = substr($url,$end);
$fp = pfsockopen($host, 80);
if(!$fp) return null;
fputs ($fp,"POST $domain HTTP/1.1\n");
fputs ($fp,"Host: $host\n");

if ($optional_headers) {
fputs($fp, $optional_headers);
}
fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
fputs ($fp,"Content-length: ".strlen($data)."\n\n");
fputs ($fp,"$data\n\n");

$response = "";
while(!feof($fp)) {
$response .= fgets($fp, 1024);
}
fclose ($fp);
return $response;
}

function do_get_request($url) {
$start = strpos($url,'//')+2;
$end = strpos($url,'/',$start);
$host = substr($url, $start, $end-$start);
$domain = substr($url,$end);
$fp = pfsockopen($host, 80);
if(!$fp) return null;
fputs ($fp,"GET $domain HTTP/1.0\n");
fputs ($fp,"Host: $host\n\n");

$response = "";
while(!feof($fp)) {
$response .= fgets($fp, 1024);
}
fclose ($fp);
$start = strpos($response, "\r\n\r\n");
return substr($response, $start);
}
?>
</body></html>

类别:高性能服务器 | 添加到搜藏 | 浏览() | 评论 (3)
 
最近读者:
 
网友评论:
1
2008-05-12 21:02 | 回复
不明白你的方法,我是有每次用手机登录,但每次都要输入用户名和密码。
 
2
2008-05-13 12:07 | 回复
试下就明白了。 上面的例子需要 wap 2.0 的手机, wap1.0的手机要改下页面将XHTML改成WML。
 
3
2008-05-13 13:29 | 回复
这都被你想到,历害!...
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu