Skip to content

Instantly share code, notes, and snippets.

@aheld
Created April 15, 2017 15:58
Show Gist options
  • Save aheld/1e4d8a5df0d13385c50018ca67d693dc to your computer and use it in GitHub Desktop.
Save aheld/1e4d8a5df0d13385c50018ca67d693dc to your computer and use it in GitHub Desktop.
<?php
/*
POST /knowledgebases/{knowledgebase ID}/generateAnswer
Host: https://westus.api.cognitive.microsoft.com/qnamaker/v1.0
Ocp-Apim-Subscription-Key: {subscriber key}
Content-Type: application/json
{"question":"hi"}
*/
// EDIT HERE ----
$path = '/knowledgebases/{knowledgebase ID}/generateAnswer';
$api_key_header = 'Ocp-Apim-Subscription-Key: {subscriber key}';
// ****
$hostPart = 'https://westus.api.cognitive.microsoft.com/qnamaker/v1.0';
$url = $hostPart . $path;
$arr = array( 'question' => urlencode($_GET['question']));
$data_string = json_encode($arr);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$api_key_header,
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment