安静
PHP技术博客

130506 Discuz X2.5 PHP 登陆发布模块

Class 类 class_dz.php

/*
* @author 80aj
* @email a@80aj.com
* @descripton: 原作者:大水车,实现discuz2.5登陆发帖
* @filename class_dz.php
*/
class discuz_post {
const LOGIN_PROGRAM = 'member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes';
const POST_PROGRAM = 'forum.php?mod=post&action=newthread&fid=';
private $host;
private $cookie_file;
/**
*
* @param unknown $host        	
*/
public function __construct($host) {
$this->host = $host;
}
/**
*
* @return unknown
*/
public function get_formhash() {
$login_url = $this->_get_Login_Url ();
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $login_url );
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$contents = curl_exec ( $ch );
curl_close ( $ch );
preg_match ( '/<input type="hidden" name="formhash" value="(.*)" \/>/isU', $contents, $matches );
if (! empty ( $matches )) {
$formhash = $matches [1];
} else {
// die('Not found the forumhash.');
}
return $formhash;
}
/**
*
* @param unknown $post        	
* @return string
*/
public function login($user, $pass) {
$login_info = array (
'username' => $user,
'password' => $pass,
'referer' => $this->host,
'questionid' => 0,
'answer' => '',
'seccodeverify' => '',
'formhash' => $this->get_formhash () 
);
$login_url = $this->_get_Login_Url ();
$cookie_file = tempnam ( './temp', 'cookie' );
$ch = curl_init ( $login_url );
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $login_info );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie_file );
curl_exec ( $ch );
curl_close ( $ch );
$this->cookie_file = $cookie_file;
return $cookie_file;
}
/**
*
* @param unknown $fid        	
* @return string
*/
public function use_cookie($fid) {
$send_url = $this->_get_Post_Url ( $fid );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $send_url );
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_COOKIEFILE, $this->cookie_file );
$contents = curl_exec ( $ch );
curl_close ( $ch );
preg_match_all ( '/<input type="hidden" name="formhash" id="formhash" value="(.*)" \/>/isU', $contents, $matches );
if (! empty ( $matches )) {
$formhash = $matches [1] [0];
} else {
$formhash = '';
}
return $formhash;
}
/**
*
* @param unknown $fid        	
* @param unknown $thread_data        	
*/
public function post_newthread($fid, $title, $content, $tags) {
$thread_data = array (
'subject' => $title,
'message' => $content,
'topicsubmit' => "yes",
'extra' => '',
'tags' => $tags,
'formhash' => $this->use_cookie ( $fid ) 
);
$send_url = $this->_get_Post_Url ( $fid );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $send_url );
curl_setopt ( $ch, CURLOPT_REFERER, $send_url );
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_COOKIEFILE, $this->cookie_file );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $thread_data );
$contents = curl_exec ( $ch );
curl_close ( $ch );
return;
}
/**
*
* @param unknown $fid        	
* @return string
*/
private function _get_Post_Url($fid) {
return $this->host . self::POST_PROGRAM . intval ( $fid );
}
/**
*
* @return string
*/
private function _get_Login_Url() {
return $this->host . self::LOGIN_PROGRAM;
}
}

测试实现类

include_once 'class_dz.php';
$host = "http://localhost/dz/"; // 服务器地址
$fid = 2; // 板块ID
$user = "admin"; // 用户名
$pass = "admin"; // 用户密码
$title = "美女大家一起来看"; // 标题
$content = "必须是美女才发,你说是不"; // 正文
$tags = "美女万岁,oh yeah"; // tags
$rc = new discuz_post ( $host );
$rc->login ( $user, $pass );
$rc->post_newthread ( $fid, $title, $content, $tags );
赞(0) 打赏
未经允许不得转载:AJ's Blog » 130506 Discuz X2.5 PHP 登陆发布模块
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏