私は自分の新しいプロジェクトの 1 つを改善するつもりです。追加したい機能の 1 つは、phpBB フォーラム ボードに新しいスレッドを投稿する機能ですが、これを行うことは可能ですか? もしそうなら、どうすればこれを行うことができますか? ありがとう。
3 に答える
phpBB データベースへの単純な挿入ステートメントでこれを実現できますが、すべてがスムーズに進むようにするために、phpBB が新しいスレッドに挿入する他の行も挿入する必要があります (ドキュメントを参照してください)。 /そのためのソースコード)。
さらに、挿入に一意の ID が必要な場合は、適切な ID を入力したことを確認する必要があります (スレッドを作成するユーザーの UserID など)。
もう 1 つの方法は、phpBB が新しいスレッドを作成するために使用する create thread 関数を公開する別の php ファイルを作成することです (これを呼び出すこともできます)。PHP ファイルへの POST/GET を許可し (POST の方が安全です)、C# から HTTP POST/GET 要求を実行します。
新しいphpファイルでは、他の誰もページを投稿/リクエストしていないことを確認するために、何らかのタイプの承認が必要になります. 特定のアクセス キーを含む必要がある特定のフィールド名をハード コーディングして、それを持たない着信ポスト/取得が無視されるようにすることができます。
2 番目の方法である imo は、phpBB がすべての難しい作業を実行できるため、より優れています。正しく接続するだけで済みます。
ただし、2 番目の方法では、セキュリティ上の問題が発生する可能性があり、phpBB はあなたがしようとしていることさえ許可しない可能性があります。(特定のメソッドを呼び出すには、ページに DEFINE("IN", "PHPBB") またはそのようなものを含める必要があると思います。これにより、実行できることにより多くの制約が課されます。
まず、phpBB サポート サイトを調べて、2 番目の部分が可能かどうかを確認し、関数の呼び出しが簡単に実行できるかどうかを調べます。
すべてのコードを書くことはしませんが、うまく動作するように構築したものをいくつかダンプできます。
1 つの方法は、webbrowser コントロールを作成し、次のようなものを作成することです。
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument doc = null;
doc = webBrowser1.Document;
//Find login text box and write user name
HtmlElement login = doc.GetElementById("username_or_email");
login.InnerText = this.login;
//Find password text box and write password
HtmlElement password = doc.GetElementById("session[password]");
password.InnerText = this.password;
// go to the submit button
webBrowser1.Document.GetElementsByTagName("input")[5].Focus();
SendKeys.Send("{ENTER}");
}
もう 1 つの方法は、http リクエストを使用することです (phpBB では確実に動作しない可能性があります)。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(twitterUrl + userID + ".xml");
string Credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(this.login + ":" + this.password));
request.Method = "POST";
request.ContentType = "application/xml";
request.AllowWriteStreamBuffering = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB6; SLCC1; .NET CLR 2.0.50727;";
request.Headers.Add("Authorization", "Basic " + Credentials);
HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();
string response = HttpWResp.StatusCode.ToString();
HttpWResp.InitializeLifetimeService();
HttpWResp.Close();
return response;
上記のコードは、Twitter にログインするために使用されます。どちらも自分の好みに合わせて変更できます。phpBB は、キャプチャとセッション検証を使用して、ユーザーがしようとしていることを正確に防止する可能性が高いことを覚えておいてください。
さて、phpBB3 へのログインは簡単な部分です。ここで使用したコードの一部を示します。
try
{
string format= "autologin=1&login=true&username={0}&password={1}";
byte[] bytes = Encoding.ASCII.GetBytes(string.Format(format, (object)HttpUtility.UrlEncode("USERNAME"), (object)HttpUtility.UrlEncode("PASSWORD")));
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://thephpbb3domain/ucp.php?mode=login");
httpWebRequest.CookieContainer = new CookieContainer(128);
httpWebRequest.Timeout = 10000;
httpWebRequest.AllowAutoRedirect = false;
httpWebRequest.UserAgent = Resources.WEB_USER_AGENT;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = (long)bytes.Length;
Stream requestStream = ((WebRequest)httpWebRequest).GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse == null)
{
int num2 = (int)MessageBox.Show(Resources.ERR_MSG_NO_DATA);
return;
}
else
{
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
streamReader.ReadToEnd().Trim();
streamReader.Close();
IEnumerator enumerator2 = httpWebResponse.Cookies.GetEnumerator();
try
{
while (enumerator2.MoveNext())
{
Cookie cookie = (Cookie)enumerator2.Current;
string str = HttpUtility.UrlDecode(cookie.Value);
if (cookie.Name.EndsWith("_k"))
{
if (cookie.Value.Length > 5)
{
break;
}
}
else if (cookie.Name.EndsWith("_data") && !str.Contains("s:6:\"userid\";i:-1;") && str.Contains("s:6:\"userid\";"))
{
}
}
}
finally
{
IDisposable disposable = enumerator2 as IDisposable;
if (disposable != null)
disposable.Dispose();
}
}
}
catch (WebException ex)
{
int num = (int)MessageBox.Show(ex.Message, "HTTP Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
これは、次のコア名前空間を使用しました
System.Net
System.Web
しかし、フォーラムにスレッドを投稿することは、本当に大きな挑戦になりました。