Keyword Discovery - API examples
This is some examples of API integration using other languages.
Contents
Perl
Example Code snippet
use CGI;
use LWP;
use XML::LibXSLT;
use XML::LibXML;
use URI::Escape;
my $username = 'username';
my $password = 'password';
my $q = new CGI;
my $ua = LWP::UserAgent->new;
my $request_url = 'http://' . uri_escape($username) . ":" . uri_escape($password)
. '@api.keyworddiscovery.com/free.php';
my %params = $q->Vars;
my $res = $ua->post($request_url,\%params);
if ($res->is_success) {
print $q->header(-charset=>'utf8');
my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
my $xml_data = $parser->parse_string($res->content);
my $style_doc = $parser->parse_file('output_template.xsl');
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($xml_data);
print $stylesheet->output_string($results);
} else {
print $q->header(-charset=>'utf8'),
$q->start_html('Auth Error Error'),
$q->h1('Auth Error'),
'Server Setup Error: invalid authentication details';
}
PHP
Example Code Snippet
<?php
$username = 'username';
$password = 'password';
$search_phrase = 'search phrase';
$request_url = sprintf(
'http://%s:%s@api.keyworddiscovery.com/free.php?q=%s',
urlencode($username),
urlencode($password),
urlencode($search_phrase)
);
$res = file_get_contents($request_url);
if ($res != '') {
$arguments = array(
'/_xml' => $res,
'/_xsl' => file_get_contents('output_template.xsl')
);
// Allocate a new XSLT processor
$xh = xslt_create();
// Process the document
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
if ($result) {
print $result;
} else {
printf('Transform error [%s] code [%d]',xslt_error($xh),xslt_errno($xh));
}
xslt_free($xh);
}
?>
C#.Net
The C# code example was provided by a 3rd party and is provided as is and may need additional coding to work.
Example Code snippet
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
XmlDocument doc = new XmlDocument();
NetworkCredential networkCredential = new NetworkCredential("username","password");
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(
"http://api.keyworddiscovery.com/free.php?q="
+ HttpUtility.UrlEncode(Request.Form["query"])
);
HttpWReq.PreAuthenticate = true;
HttpWReq.Credentials = networkCredential;
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
if (HttpWResp.StatusCode == HttpStatusCode.OK) {
doc.Load(HttpWResp.GetResponseStream());
}
XslTransform trans = new XslTransform();
trans.Load(Server.MapPath("output_template.xsl"));
xml1.Document = doc;
xml1.Transform = trans;
}
</script>
<asp:Xml id="xml1" runat="server" />
Example output_template.xsl XSL file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY sp "	">
<!ENTITY nbhy "‑">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" version="4.0" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Query Results</title>
</head>
<body>
<center>
<form method="post" name="search_form" action="free.cgi">
<p class="copy"><b>Enter a keyword:</b><br />
<input type="text" name="query" /> <input type="submit" value="Search" /></p>
<xsl:for-each select="resultset/results">
<div style="float: left;" align="left">
<h3 class="lhcopy">Query: <xsl:value-of select="@query" /></h3>
<table class="sortable" id="{generate-id(@query)}" border="0" cellspacing="1" style="margin-bottom: 10px;">
<thead>
<tr>
<th align="center">query</th>
<th align="center" sortfn="ts_sort_currency">total</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="r">
<tr class="row{position() mod 2}">
<td class="q"><a href="#"><xsl:attribute name="onclick">document.search_form.query.value = '<xsl:value-of select="@q" />'; document.search_form.submit();</xsl:attribute><xsl:value-of select="@q" /></a></td>
<td class="t"><xsl:value-of select="format-number(@t, '###,###')" /></td>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
</xsl:for-each>
</form>
<br/><br/>
<div id="providedby" class="copy" style="clear: both;">
Keywords provided by KeywordDiscovery.com<br/>
For more results visit <a href="http://www.keyworddiscovery.com">KeywordDiscovery.com</a>
</div>
</center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|