PHP Sample ApplicationThis is a featured page

Our Wordpress prototype was written in PHP. By taking a closer look at the calls our Wordpress plug-in makes, you can get an idea of how your PHP application would integrate with Injected.

The following function ensures the current user is logged in to the Wetpaint Server:

function wetpaint_init() {
global $wetpaint_api_host, $wetpaint_developer_key, $wetpaint_developer_secret,$wetpaint_developer_content_namespace, $wetpaint_ticket;

global $user_login, $user_email, $user_level, $user_url, $user_identity;
get_currentuserinfo();
if (is_user_logged_in() && !(isset($_COOKIE['wetpaint-togo']))) {
$submit_vars["key"] = $wetpaint_developer_key;
$submit_vars["ns"] = $wetpaint_developer_content_namespace;
$submit_vars["output"] = "api";

$ts = time();
$sig = hash_hmac( "sha1", $wetpaint_developer_key . $user_login . $ts, $wetpaint_developer_secret, FALSE );
$submit_vars["cred.ts"] = $ts;
$submit_vars["cred.sig"] = $sig;

$submit_vars["user.userId"] = $user_login;
$submit_vars["user.displayName"] = $user_identity;
$submit_vars["user.email"] = $user_email;
$submit_vars["user.emailOptIn"] = "true";
$submit_vars["user.profileUrl"] = $user_url;
if ( $user_level >= 5 ) {
$submit_vars["user.role"] = "moderator";
}
$url = $wetpaint_api_host . '/UserService/login.do';
$client = new Snoopy();
@$client->submit($url, $submit_vars);
$xml = $client->results;
if( preg_match( "/<ticket>(.*)<\/ticket>/", $xml, $match_array ) == 1 ) {
$wetpaint_ticket = $match_array[1];
setcookie("wetpaint-togo",$match_array[1], time()+3600*12 );
} else {
#print "ERROR->" . $xml;
}
}
if (isset($_COOKIE['wetpaint-togo'])) {
$wetpaint_ticket = $_COOKIE['wetpaint-togo'];
}
}

function wetpaint_logout() { setcookie("wetpaint-togo", "", -1); }



The following function writes the javascript needed for the client application:

function wetpaint_header() {
global $wetpaint_developer_key, $wetpaint_api_host, $wetpaint_developer_content_namespace, $wetpaint_ticket;

$content .= "\t<script src=\"" . $wetpaint_api_host . "/JavaScriptService/getBootstrap.do?" . $wetpaint_developer_key . "\" type=\"text/javascript\"></script>\r";
$content .= "\t<script type=\"text/javascript\">\r";
$content .= "\t\tif ( window.WPCAPI ) {\r";

if ( isset( $wetpaint_ticket ) ){

$content .= "\t\t\tWPCAPI.setLoginTicket('" . $wetpaint_ticket . "');\r";

}

$content .= "\t\t\tWPCAPI.setDeveloperKey('" . $wetpaint_developer_key . "');\r";
$content .= "\t\t\tWPCAPI.setNamespace('" . $wetpaint_developer_content_namespace . "');\r";
$content .= "\t\t}\r";
$content .= "\t</script>\r";

print $content;

}


The above function will generate the output shown below, as required for the client application (refer to step 3 of the integration guide). The first script tag requests the bootstrap, the second sets the site properties.


//function will generate these tags if user has a ticket
<script src="http://wapi.wetpaint.com/JavaScriptService/getBootstrap.do?
yourdeveloperkey" type="text/javascript"></script>
<script type="text/javascript">
if ( window.WPCAPI ) {
WPCAPI.setLoginTicket("s0m3tick3t-received-during-login"); //see wetpaint_init() function above.
WPCAPI.setDeveloperKey("yourdeveloperkey");
WPCAPI.setNamespace("yournamespace");
}
</script>


The following functions retrieve user generated content:

function wetpaint_content( $content ) {
global $post, $wetpaint_developer_key, $wetpaint_api_host, $wetpaint_developer_content_namespace, $wetpaint_ticket;
if ( !( is_home() || is_single() || is_category() || is_date() || is_archive() || is_search() ) ) { return $content; }
$cellId = $_REQUEST['cellId'];
$displayName = $_REQUEST['displayName'];
$cellUrl = $_REQUEST['cellUrl'];
if( $cellId != "" ) $content = ""; else $cellId = $post->ID . '-1';
if( $displayName == "" ) $displayName = $post->post_title;
if( $displayName == "" ) $displayName = "Untitled";
if( $cellUrl == "" ) $cellUrl = $_SERVER["PHP_SELF"];
$wp_content = wetpaint_load_content( $cellId, $displayName, $cellUrl );
if( $wp_content ) {
$content.= "<!-- start wetpaint -->\r";
$content.= $wp_content;
preg_match("/<!--content-->(.*)<!--end-->/", $wp_content, $content_array );
preg_match("/<!--version (.*)-->/", $wp_content, $version_array );
$content.="<!-- end wetpaint -->\r";
} else {
$content.="<small>Unable to retrieve Wetpaint content (server unavailable).</small>";
}
return $content;
}

function wetpaint_load_content( $cellId, $displayName, $cellUrl ) {
global $wetpaint_api_host, $wetpaint_developer_key, $wetpaint_developer_secret, $wetpaint_developer_content_namespace, $post;
$url = $wetpaint_api_host . '/CellService/getCell.do?key=' . urlencode( $wetpaint_developer_key) . '&ns=' . urlencode( $wetpaint_developer_content_namespace ) . '&cell.cellId=' . urlencode( $cellId ) . '&cell.displayName=' . urlencode( $displayName ) . '&cell.url=' . urlencode( $cellUrl ) . '&output=html';
$client = new Snoopy();
@$client->fetch($url);
if ( $client->timed_out ) return "";#"Time out calling -> " . $url;
if ( $client->status != 200 ) return "";#"Error calling: " . $url . " Results: (" . $client->results . ")";
$results = $client->results;
if( $results != "" ) $results .= '<div><em>' . wetpaint_addpage_link($cellId, $displayName) . '</em></div>';
return $results;
}

Data for testing login is available here.


boe_wetpaint
boe_wetpaint
Latest page update: made by boe_wetpaint , Sep 2 2008, 5:37 PM EDT (about this update About This Update boe_wetpaint Modified to include developer key on getBootstrap.do - boe_wetpaint

8 words added
3 words deleted

view changes

- complete history)
More Info: links to this page
There are no threads for this page.  Be the first to start a new thread.

Related Content

  (what's this?Related ContentThanks to keyword tags, links to related pages and threads are added to the bottom of your pages. Up to 15 links are shown, determined by matching tags and by how recently the content was updated; keeping the most current at the top. Share your feedback on Wetpaint Central.)