Using Clarifai via PHP and Curl

Update March 2023: The code below will not work after March 2023 as Clarifai has changed their authentication system. Please have a look at the index.php file in the CCTV project mentioned below (https://github.com/marcconrad/myCCTV) – search for ‘curl’

Update: link to Clarifai’s conference – Perceive 2020 – https://www.youtube.com/playlist?list=PLs05ayZZ-2yg02sEAQ2Ffah2ilDYmc4Ch 

Update: I have now a project on github that is a cctv that uses Clarifai to identify concepts and make animated gifs: https://github.com/marcconrad/myCCTV

php code, to find out what is in an image:

$random_string=substr(str_shuffle(MD5(microtime())), 0, 5);

echo ‘<h1>’.$random_string.'</h1>’;

$imgurl=”http://perisic.com/four/train/img.php?s=”.$random_string.”&t=2.jpg”;

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_VERBOSE, ‘1’);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_URL, “https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_exec returns the value

$headers = array(
‘Content-Type: application/json’,
“Authorization: xyzabcd”
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$fields = ‘{“inputs”:[{“data”:{“image”:{“url”:”‘.$imgurl.'”}}}]}’; // Your image here.
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

// grab URL and pass it to the browser
$result = curl_exec($ch);

// echo ‘<h2>’.$result.'</h2>’;

$mydata= json_decode($result, true);

$concepts = $mydata[“outputs”][0][“data”][“concepts”];

// var_dump($concepts);

$conceptlist=””;
for($i=0;$i < sizeof($concepts); $i++ ) {
if($i != 0 ) { $conceptlist .= ‘, ‘; }
$value = $concepts[$i];
$conceptlist .= $value[“name”];
}

echo “<h3>Concepts: “;
echo $conceptlist;
echo “</h3>”;

// close cURL resource, and free up system resources
curl_close($ch);

echo ‘<img src=”‘.$imgurl.'” alt=”‘.$conceptlist.'” title=”‘.$conceptlist.'”>’;

Finally: C:\Users\vim\curl\curl.exe –verbose –libcurl tmp1.c -H “Authorization: Key xyzabcd” -H “Content-Type: application/json” –data “{“”inputs””:[{“”data””:{“”image””:{“”url””:””http://www.perisic.com/five/imgfive.php?s=helbo&w=10″”}}}]}”  https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs

(You must replace xyzabcd with your own API key)