Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newbold/cc9505f5ad1e2665af67947493c5fdb7 to your computer and use it in GitHub Desktop.
Save newbold/cc9505f5ad1e2665af67947493c5fdb7 to your computer and use it in GitHub Desktop.
Simple Watson Visual Recognition implementation with Twilio MMS in PHP. Twilio will POST an image URL through via MediaUrl0, so it's easy to grab that and send it on to Watson for recognition.
if(isset($_REQUEST['MediaUrl0'])) {
$img = $_REQUEST['MediaUrl0'];
$apikey = 'put_a_real_key_here';
$data = `curl -u "apikey:$apikey" "https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?url=$img&version=2018-03-19"`;
$data = json_decode($data);
$thing = $data->images[0]->classifiers[0]->classes[0]->class;
$intros = array('Hey', 'Cool', 'Jeepers', 'Wow', 'Awesome', 'Nifty', 'Woah', 'Yikes', 'Well', 'Eh', 'Um', 'Uh', 'Zoinks');
$intro = array_rand($intros);
$intro = $intros[$intro];
$vowels = array('a','e','i','o','u');
if(in_array($thing{0}, $vowels)) $article = 'an';
else $article = 'a';
echo '<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Message>'.$intro.', that looks like '.$article.' '.$thing.'.</Message>
</Response>';
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment