首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 模擬登錄 QQ 空間自動發布日誌:構造 POST 請求與 Cookie 管理

     編輯:PHP 綜合知識

核心思路

通過 PHP 模擬瀏覽器發送 POST 請求至 QQ 空間發文接口,包含 Cookie 文件持久化、關鍵參數 g_tk 處理、Referer 偽造等完整流程。

通用請求函數

function request($url, $referer='', $postdata='', $cookie='', $cookiefile='') {
    $header = "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)\r\n";
    $header .= "Referer: $referer\r\n";
    $header .= "Cookie: $cookie\r\n";
    $method = $postdata ? 'POST' : 'GET';
    $opts = ['http' => ['method'=>$method, 'header'=>$header, 'content'=>$postdata]];
    $context = stream_context_create($opts);
    $yuanma = file_get_contents($url, false, $context);
    // 保存 Cookie 到文件
    if ($cookiefile) {
        $response = implode("\r\n", $http_response_header);
        preg_match_all("/Set-Cookie:(.*?)\r\n/", $response, $cookie_arr);
        if (!empty($cookie_arr[1])) file_put_contents($cookiefile, implode(';', $cookie_arr[1]));
    }
    return $yuanma;
}

表單輸入與關鍵參數

  • qq:QQ 號碼。
  • g_tk:關鍵防 CSRF Token,需從瀏覽器開發者工具抓包獲取(發文接口 URL 參數 g_tk=123456789)。
  • title、content:日誌標題與內容。

發文邏輯

$cookiefile = dirname(__FILE__) . '\qq_cookie.txt';
if (!file_exists($cookiefile)) { file_put_contents($cookiefile, ''); die('請填寫抓包的 Cookie'); }
$cookie = file_get_contents($cookiefile);

$qq = $_POST['qq']; $g_tk = $_POST['g_tk'];
$title = $_POST['title']; $content = $_POST['content'];
$category = '個人日記';
$fabiao = "https://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=$g_tk";
$referer = 'https://ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html';
$r1 = "https://user.qzone.qq.com/$qq/infocenter";
$postdata = "uin=$qq&category=".urlencode($category)."&title=".urlencode($title).
            "&content=".urlencode($content).
            "&html=".urlencode('
'.$content.'
'). "&tweetflag=0&cb_autograph=1&topflag=0&needfeed=0&g_tk=$g_tk". "&_fp_refer=".urlencode("https://ctc.qzs.qq.com/qzone/newblog/v5/editor.html?source=1|https://ctc.qzs.qq.com/qzone/newblog/v5/editor.html?source=1

注意事項

  • QQ 空間接口頻繁變更,g_tk 算法可能調整,需定期維護。
  • 模擬登錄屬灰色地帶,請遵守平台服務條款,建議使用官方 Open API。
  • Cookie 失效需重新抓包,生產環境建議實現自動掃碼登錄流程。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved