# Solved: IPB PHP API class error



## TheRobatron (Oct 25, 2007)

Sorry about all the TLAs . I have Invision Power Board installed on my site, and it comes with an API but it isn't documented. Does anyone else use the IPB API?

I am having a problem with the API named api_topics_and_posts.php when trying to create a new topic. I am using the example code from the API file - this is the exact code I am using, with nothing else in the script:


```
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require ("forums/sources/api/api_topics_and_posts.php");
$api = new api_topics_and_posts();
$api->ipsclass =& $this->ipsclass;				// ***Line 7***
$api->set_author_by_id( 2 );
$api->set_post_content("[B]Hello World![/B] :D");
$api->set_forum_id( 4 );
$api->set_topic_title('Hello World');
$api->set_topic_description('I am the description');
$api->set_topic_state('open');
$api->create_new_topic();
?>
```
The error I get is:
_Fatal error: Using $this when not in object context in /hsphere/local/home/********/*****************/testapi.php on line 7_

I've never used classes in PHP but I would have thought that $this refers to the current class.

Thanks for any help.


----------



## TheRobatron (Oct 25, 2007)

Bump. I heard that the &= operator is not supported in PHP 5 (the version I'm using) but I can't find any documentation on php.net or the web, mainly because search engines don't like searching for special characters. Is there an alternative operator or method of doing this?


----------



## TheRobatron (Oct 25, 2007)

Forget what I said about the &= operator - I was completely wrong. It turns out there is a bug in PHP 5.2.3 that stops files from being included that are in other directories whilst within nested classes (or something like that). My host isn't likely to change their version of PHP soon, so I decided to write my own API. I'll post it here in case anyone finds it useful:


```
function ipb_new_topic($title,$description,$post,$starter_name,$starter_id,$forum_id,$ip_addr) {

		$epoch = strtotime("now");

		// Perform basic input validation
		$badStrings = array ("<",">","'",'"');
		$replacements = array ("<",">","'",""");

		$title = str_replace($badStrings,$replacements,$title);
		$description = str_replace($badStrings,$replacements,$description);
		$post = str_replace($badStrings,$replacements,$post);
		$starter_name = str_replace($badStrings,$replacements,$starter_name);
		$ip_addr = str_replace($badStrings,$replacements,$ip_addr);

		// Add 
 tags into the post
		$post = str_replace("\n","
",$post);

		// Replace with your details
		$host = "";
		$user = "";
		$pass = "";
		$con = mysql_connect($host,$user,$pass);
		mysql_select_db("",$con);

		$query = "SELECT `pid` FROM `ibf_posts` ORDER BY `pid` DESC LIMIT 1";

		if(!$result = mysql_query($query,$con)) {
			die ("Could not select post ID: ".mysql_error());
		}

		while ($arr = mysql_fetch_array($result)) {
			$pid = $arr['pid'] + 1;
		}

		$query = "INSERT INTO `ibf_topics` (title, description, state, posts, starter_id, start_date, last_poster_id, last_post, icon_id, starter_name, last_poster_name, poll_state, last_vote, views, forum_id, approved, author_mode, pinned, moved_to, total_votes, topic_hasattach, topic_firstpost, topic_queuedposts, topic_open_time, topic_close_time, topic_rating_total, topic_rating_hits)
		VALUES ('$title', '$description', 'open', 0, $starter_id, $epoch, $starter_id, $epoch, 0, '$starter_name', '$starter_name', 0, 0, 0, $forum_id, 1, 1, 0, NULL, 0, 0, $pid, 0, 0, 0, 0, 0)";

		if(!$result = mysql_query($query,$con)) {
			die ("Could not create new topic: ".mysql_error());
		}

		$query = "SELECT `tid` FROM `ibf_topics` ORDER BY `tid` DESC LIMIT 1";

		if(!$result = mysql_query($query,$con)) {
			die ("Could not select topic ID: ".mysql_error());
		}

		while ($arr = mysql_fetch_array($result)) {
			$tid = $arr['tid'];
		}

		$query = "INSERT INTO `ibf_posts` (append_edit, edit_time, author_id, author_name, use_sig, use_emo, ip_address, post_date, icon_id, post, queued, topic_id, post_title, new_topic, edit_name, post_key, post_parent, post_htmlstate, post_edit_reason) 
		VALUES (0, NULL, $starter_id, '$starter_name', 1, 0, '$ip_addr', $epoch, 0, '$post', 0, $tid, NULL, 1, NULL, 0, 0, 0, '')";

		if(!$result = mysql_query($query,$con)) {
			die ("Could not create new post: ".mysql_error());
		}

		// Add a post to the user
		$query = "UPDATE `ibf_members` SET posts = posts + 1";

		if(!$result = mysql_query($query,$con)) {
			echo "Could not update member ".$starter_name."'s (ID = ".$starter_id.") post count: ".mysql_error();
		}

		if (!isset($error)) {
			return true;
		} else {
			return $error;
		}
	}
```


----------

