如题,数据包是用FIDDLER抓取的正常发送时数据包如上依照这个格式写的PHP如下<?php
$srv_ip = 'weibo.com';
$srv_port = 80;
$fp = '';
$errno = 0;
$errstr = '';
$timeout = 10;
$weibo_time=explode(' ',microtime());
$weibo_time=$weibo_time[1].$weibo_time[0][2].$weibo_time[0][3].$weibo_time[0][4];
$url = "http://weibo.com/aj/mblog/add?_wv=5&__rnd=$weibo_time";
$fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout);
if (!$fp){
echo('fp fail');
}
$content_length = strlen($post_str);
$post_header = "POST $url HTTP/1.1\r\n";
$post_header.="Host: weibo.com\r\n";
$post_header.="Connection: keep-alive\r\n";
$post_header.="Content-Length: ".$content_length."\r\n";
$post_header.="Origin: http://weibo.com\r\n";
$post_header.="X-Requested-With: XMLHttpRequest\r\n";
$post_header.="User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 SE 2.X MetaSr 1.0\r\n";
$post_header.="Content-Type: application/x-www-form-urlencoded\r\n";
$post_header.="Accept: */*\r\n";
$post_header.="Referer: http://weibo.com/111690558?topnav=1&wvr=5\r\n";
$post_header.="Accept-Encoding: gzip,deflate,sdch\r\n";
$post_header.="Accept-Language: zh-CN,zh;q=0.8\r\n";
$post_header.="Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n";
$post_header.="Cookie: SINAGLOBAL=9465448379050.94.1363149364838; __utma=182865017.444875141.1368123665.1368123665.1368123665.1; __utmz=182865017.1368123665.1.1.utmcsr=weibo.com|utmccn=(referral)|utmcmd=referral|utmcct=/111690558; un=15060958332; wvr=5; tips_1690464047=1; USRHAWB=usrmdins540_129; SUE=es%3D507eea8961adc4be8745ced26692cecf%26ev%3Dv1%26es2%3D60cde3f363e58640ff977cc494b9b324%26rs0%3DHOCml5t36I5CpM5fJt6Zoy8AOhOq%252FrcttFUN3DWVH1vp%252FHzTj0d2WIlTRp8FTL8AfkFeIQhGdz873EpBI85zZxQXH%252FZlFrNQ8v3cFXgDWIi%252BjUink2UnhThidtqeofdZ83%252BE7tm6z2KhuWJ1h6yaQ5vUIrX39if85caUor0EJzg%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1374020329%26et%3D1374106729%26d%3Dc909%26i%3D9e53%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D2%26st%3D0%26uid%3D1690464047%26name%3D15060958332%2540sina.cn%26nick%3D15060958332%26fmp%3D%26lcp%3D; SUS=SID-1690464047-1374020329-GZ-57e6f-e2b33deb6283a87d733e40d63e8f7ee9; ALF=1376127464; SSOLoginState=1374020329; USRUG=usrmdins1540_23; _s_tentry=login.sina.com.cn; Apache=7815071931108.832.1374020351610; ULV=1374020351624:608:64:33:7815071931108.832.1374020351610:1373988030276; UOR=,weibo.com,bbs.fzu.edu.cn; SinaRot_wb_r_topic=94; arp_scroll_position=0\r\n\r\n";
$post_header.="text=test&pic_id=&rank=0&rankid=&_surl=&hottopicid=&location=home&module=stissue&_t=0";
echo $post_header;
fwrite($fp,$post_header);
$inheader = 1;
while(!feof($fp)){
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
unset ($line);
?>
得到的结果是<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>可是把这个数据包复制到FIDDLER等工具中发送,明明就是成功的,是PHP哪里有问题了吗有的文章中说数据包POST_STR后面部分应该加上\r\n\r\n的,我也都试过了,加两个,加一个,不加,都是失败的PS:我是在命令行下用的PHP
PHP微博Windows
$srv_ip = 'weibo.com';
$srv_port = 80;
$fp = '';
$errno = 0;
$errstr = '';
$timeout = 10;
$weibo_time=explode(' ',microtime());
$weibo_time=$weibo_time[1].$weibo_time[0][2].$weibo_time[0][3].$weibo_time[0][4];
$url = "http://weibo.com/aj/mblog/add?_wv=5&__rnd=$weibo_time";
$fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout);
if (!$fp){
echo('fp fail');
}
$content_length = strlen($post_str);
$post_header = "POST $url HTTP/1.1\r\n";
$post_header.="Host: weibo.com\r\n";
$post_header.="Connection: keep-alive\r\n";
$post_header.="Content-Length: ".$content_length."\r\n";
$post_header.="Origin: http://weibo.com\r\n";
$post_header.="X-Requested-With: XMLHttpRequest\r\n";
$post_header.="User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 SE 2.X MetaSr 1.0\r\n";
$post_header.="Content-Type: application/x-www-form-urlencoded\r\n";
$post_header.="Accept: */*\r\n";
$post_header.="Referer: http://weibo.com/111690558?topnav=1&wvr=5\r\n";
$post_header.="Accept-Encoding: gzip,deflate,sdch\r\n";
$post_header.="Accept-Language: zh-CN,zh;q=0.8\r\n";
$post_header.="Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n";
$post_header.="Cookie: SINAGLOBAL=9465448379050.94.1363149364838; __utma=182865017.444875141.1368123665.1368123665.1368123665.1; __utmz=182865017.1368123665.1.1.utmcsr=weibo.com|utmccn=(referral)|utmcmd=referral|utmcct=/111690558; un=15060958332; wvr=5; tips_1690464047=1; USRHAWB=usrmdins540_129; SUE=es%3D507eea8961adc4be8745ced26692cecf%26ev%3Dv1%26es2%3D60cde3f363e58640ff977cc494b9b324%26rs0%3DHOCml5t36I5CpM5fJt6Zoy8AOhOq%252FrcttFUN3DWVH1vp%252FHzTj0d2WIlTRp8FTL8AfkFeIQhGdz873EpBI85zZxQXH%252FZlFrNQ8v3cFXgDWIi%252BjUink2UnhThidtqeofdZ83%252BE7tm6z2KhuWJ1h6yaQ5vUIrX39if85caUor0EJzg%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1374020329%26et%3D1374106729%26d%3Dc909%26i%3D9e53%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D2%26st%3D0%26uid%3D1690464047%26name%3D15060958332%2540sina.cn%26nick%3D15060958332%26fmp%3D%26lcp%3D; SUS=SID-1690464047-1374020329-GZ-57e6f-e2b33deb6283a87d733e40d63e8f7ee9; ALF=1376127464; SSOLoginState=1374020329; USRUG=usrmdins1540_23; _s_tentry=login.sina.com.cn; Apache=7815071931108.832.1374020351610; ULV=1374020351624:608:64:33:7815071931108.832.1374020351610:1373988030276; UOR=,weibo.com,bbs.fzu.edu.cn; SinaRot_wb_r_topic=94; arp_scroll_position=0\r\n\r\n";
$post_header.="text=test&pic_id=&rank=0&rankid=&_surl=&hottopicid=&location=home&module=stissue&_t=0";
echo $post_header;
fwrite($fp,$post_header);
$inheader = 1;
while(!feof($fp)){
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
unset ($line);
?>
得到的结果是<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>可是把这个数据包复制到FIDDLER等工具中发送,明明就是成功的,是PHP哪里有问题了吗有的文章中说数据包POST_STR后面部分应该加上\r\n\r\n的,我也都试过了,加两个,加一个,不加,都是失败的PS:我是在命令行下用的PHP
PHP微博Windows
运行后会自动评论指定微博地址的最新一条微博,10秒钟评论一次。评论内容最后我加了很长的随机串来防止被新浪过滤。运行环境:ruby1.9+
#! /usr/local/bin/ruby -w
# -*- coding: UTF-8 -*-require 'net/http'class Auto_comment @@comment_url = 'http://weibo.com/aj/comment/add?_wv=5&__rnd=13811801512' @@headers = {
'Cookie' => '[这里换成你的cookie]',
'Referer' => 'http://weibo.com',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22',
'Content-Type' => 'application/x-www-form-urlencoded'
}
def exec weibo, content
@weibo = weibo
@content = content
check_new_post
end
private
def create_salt
chars = ("a".."z").to_a
newpass = ""
1.upto(100) { |i| newpass << chars[rand(chars.size-1)] }
newpass
end def check_new_post uri = URI.parse @weibo req = Net::HTTP::Get.new uri.request_uri, @@headers
res = Net::HTTP.start uri.host, uri.port do |http|
http.request(req)
end mid_list = res.body.scan( /mid=\\\"(.*?)\\\"/ )
mid = mid_list.first[0].to_s comment mid
end def comment mid postfields = {
:act => 'post',
:mid => mid,
:forward => '0',
:isroot => '0',
:content => @content+"_防新浪过滤重复内容随机串{"+create_salt+"}",
:type => 'big',
:location => 'mblog',
:module => 'bcommlist',
:tranandcomm => '1',
:_t => '0'
} uri = URI.parse @@comment_url
http = Net::HTTP.new( uri.host, 80)
field = '' postfields.each do |k, v|
field += "#{k}=#{v}&"
end
field.chop!
http.post uri.request_uri, field, @@headers
end
end
#要评论的微博地址
weibo = 'http://weibo.com/u/1777627917'
content = '自动评论程序测试'comment = Auto_comment.new
#每10秒钟评论一次
loop do
comment.exec weibo, content
sleep 10
end
cookie我是从chrome的审查元素里面取到的。如果确定cookie没问题的话,你把发送的http头精简下,跟我一样只发送4个域。因为你那结果是301,所以我还是怀疑是你cookie验证的部分没有通过。
之前我也用php写过几个操作微博的,如自动发微博,自动关注别人等等,不过代码没有存。所以没法发出来给你参考了。
post的数据不应该加入header,应该用curl相关参数处理
也没有看到 Content-length:nnn一般的说,post数据的长度,在 Connection:close 前声明,数据体附加在正文中,即Connection:close之后
正常发送的时候数据是这样的
另外PHP中的也是和正常发送的一样是KEEP-ALIVE在第三行