How can I use a PHP script to index URLs or documents?

Once you download the PHP examples from our Downloads page (http://www.searchblox.com/developers/downloads), you will need to update the following fields in your PHP script to ensure you can index URLs/documents to SearchBlox:

* SearchBlox installation path (http://localhost:8080/searchblox/api/rest/add)
* SearchBlox API key
* SearchBlox custom collection name (Please create a custom collection once you install SearchBlox.)

Refer to the Developer page for parameters that can be passed through the XML message: http://www.searchblox.com/developers/api.

Here is a sample PHP script that can be used to index URLs or documents (filesystem can be specified only when using the same server as SearchBlox):

<pre><code>&lt;html>
<?php
// Example code adding a document using rest API
function searchblox_index() {
$xml_input='<?xml version="1.0" encoding="utf-8"?>
&lt;searchblox apikey="C1E6FCF058DC2D79F72C9CDF30B5B1AF">
&lt;document colname="ht_sumod" location="http://edition.cnn.com/">
&lt;url>http://edition.cnn.com </url&gt;
&lt;title boost="1"> My custum collection</title&gt;
&lt;keywords boost="1">keywords</keywords&gt;
&lt;category>category1</category&gt;
&lt;contenttype>HTML</contenttype></document></searchblox&gt;';
echo $xml_input;
$url="http://localhost:8080/searchblox/api/rest/add";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array('Content-Type: text/xml'));
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS,trim($xml_input));
$body = curl_exec($ch);
if(curl_errno($ch))
{
echo 'searchblox_curl_warning';
curl_close($ch);
$error_count++;
}
else
{
curl_close($ch);
$xml = simplexml_load_string($body);
global $statuscode;
$statuscode = (string) $xml->statuscode;
if (intval($statuscode)>=100 AND intval($statuscode)<=701)
{
echo 'searchblox_xml_warning';
if ( intval($statuscode) == 100 )
{
$error_count = 0; // After first succesful connection, lets reset error counter
}
//else $error_count++; // Skip if document cannot be indexed
}
elseif ( $statuscode <> '' )
{
echo 'searchblox_unknown_reply';
//$error_count++; // Skip if document cannot be indexed
}
}
}
if ( isset($_POST['sb_index'])) searchblox_index( );
?>
&lt;body&gt;
&lt;div style="width:25%; float:left;"&gt;
&lt;form name="sb_index_form" method="post" action=""&gt;
&lt;input type="submit" id="sb_index" name="sb_index" value="Index Document" /&gt;
&lt;/form>&lt;/div></body&gt;
&lt;/html&gt;</code></pre>

Have more questions? Submit a request

Comments