GET
To List your current indexes with ‘standard’ flavor use a GET request to the List Indexes API.
$url_listindexes = 'https://api.idolondemand.com/1/api/sync/listindexes/v1';
$params1 = 'flavor=standard&apikey='.$apikey;
$response = file_get_contents($url_listindexes .'?'.$params1);
if($response) { $json = json_decode($response); if($json){ $indexes = $json->index; } return $indexes; }
POST
POST multipart/form-data with native PHP
function send_multipart_post_message($sync_or_async, $json1){
$url = "https://api.idolondemand.com/1/api/".$sync_or_async."/addtotextindex/v1";
// using WordPress custom functions to retrieve index and apikey
$index1 = wp_idolondemand_get_setting('index');
$apikey = wp_idolondemand_get_setting('apikey');
$eol = "\r\n";
$data = '';
$mime_boundary=md5(time());
//
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="apikey"' . $eol . $eol;
$data .= $apikey . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="index"' . $eol . $eol;
$data .= $index1 . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="json"; filename="allposts.json"' . $eol;
$data .= 'Content-Type: application/json' . $eol;
$data .= 'Content-Transfer-Encoding: base64' . $eol . $eol;
$data .= base64_encode($json1) . $eol;
// alternatively use 8bit encoding
//$data .= 'Content-Transfer-Encoding: 8bit' . $eol . $eol;
//$data .= $json1 . $eol;
$data .= "--" . $mime_boundary . "--" . $eol . $eol;
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data; boundary=' . $mime_boundary,
'content' => $data
//'proxy' => 'tcp://localhost:8888' //use with Charles to catch http traffic
));
$ctx = stream_context_create($params);
$response = file_get_contents($url, FILE_TEXT, $ctx);
return $response;
}
POST with Client URL Library
For more information on the Client URL Library in PHP, go here.
To add content to your text index, use the Add to Text Index API as following example, which creates a request in json to add a WordPress post to the text index.
// dont index html tags
// but IDOL OnDemand strips HTML tags by default
// $post_content = strip_tags($post_content);
$post_content = json_encode($post_content);
//
// use WordPress functions to get content
$post_tags = wp_get_post_tags($post_id);
$post_categories = wp_get_post_categories($post_id);
$json1 = "{\"document\":[".
"{" .
"\"title\":\"". $post_title ."\"," .
"\"reference\":\"". $post_url ."\"," .
"\"content\":". $post_content ."," .
"\"tags\":[\"". $post_tags ."\"]," .
"\"categories\":[\"". $post_categories ."\"]" .
"}".
"]}";
//
$url = "https://api.idolondemand.com/1/api/sync/addtotextindex/v1";
$args['apikey']= $apikey;
$args['json'] = $json1;
$args['index']= $index1;
$request_json = json_encode($args);
$client_session = curl_init($url);
curl_setopt($client_session, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($client_session, CURLOPT_POSTFIELDS, $request_json);
curl_setopt($client_session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($client_session, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($request_json)) );
$response = curl_exec($client_session);
if ($response) {
$json = json_decode($response);
}
POST with PHP extensions
The easiest way to send a POST request in PHP is to use a HTTP client library, but often webservers do not come with pre-installed HTTP clients.
- PEAR 2 HTTP Client
- PECL extension for Extended HTTP support
It’s very very helpful.