簡體   English   中英

使用PHP和cURL發布到phpBB2論壇

[英]Posting to phpBB2 forum with PHP and cURL

我正在嘗試發布到使用PHP和cURL在localhost上運行的phpBB2論壇。 我已經處理好登錄了,這只是我無法理解的帖子。

這是我的代碼:

<?php

$cookieFile = 'C:\xampp\htdocs\cookies\\' . uniqid(true) . '.txt';

// Login
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/phpbb2/login.php');
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
$postVars = array('username' => 'admin', 'password' => 'password', 'autologin' => 'on', 'login' => 'Log in');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postVars);
$resp = curl_exec($curl);
curl_close($curl);

// Parse sid from cookie file
preg_match('/phpbb2mysql_sid\t(.*)/', file_get_contents($cookieFile), $match);
$sId = $match[1];

// Post
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/phpbb2/posting.php?mode=newtopic&f=1');
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
$postVars = array('subject' => 'Test post',
                  'message' => 'Test post, please ignore.',
                  'sid' => $sId,
                  'f' => 1,
                  'post' => 'Submit');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postVars);
$resp = curl_exec($curl);
curl_close($curl);

echo $resp;

cURL可以很好地設置cookie,而且我知道我通過POST請求發送的sid參數是正確的,因為它與數據庫中的參數相同。 但是,當我運行此代碼時,phpBB吐出此錯誤: Invalid Session. Please resubmit the form. Invalid Session. Please resubmit the form.

我不明白 登錄后,我將抓取cookie,將其與POST請求一起發送以創建一個新主題,但它顯示無效會話。

這里可能出什么問題了?

我會猜測是因為您執行

curl_close($curl); 
$curl = curl_init();

登錄后,然后再發布。 您要刪除這兩行並繼續使用相同的卷曲手柄。

順便說一句:您的phpbb登錄代碼對我來說效果很好...;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM