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)

One thought on “Using Clarifai via PHP and Curl

  1. /********* Sample code generated by the curl command line tool **********
    * All curl_easy_setopt() options are documented at:
    * https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
    ************************************************************************/
    #include

    int main(int argc, char *argv[])
    {
    CURLcode ret;
    CURL *hnd;
    struct curl_slist *slist1;

    slist1 = NULL;
    slist1 = curl_slist_append(slist1, “Authorization: Key xyzabcd”);
    slist1 = curl_slist_append(slist1, “Content-Type: application/json”);

    hnd = curl_easy_init();
    curl_easy_setopt(hnd, CURLOPT_URL, “https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs”);
    curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, “{\”inputs\”:[{\”data\”:{\”image\”:{\”url\”:\”http://www.perisic.com/five/imgfive.php?s=helbo&w=10\”}}}]}”);
    curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)94);
    curl_easy_setopt(hnd, CURLOPT_USERAGENT, “curl/7.53.1”);
    curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
    curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
    curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
    curl_easy_setopt(hnd, CURLOPT_CAINFO, “C:\\Users\\vim\\curl\\ca-bundle.crt”);
    curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

    /* Here is a list of options the curl code used that cannot get generated
    as source easily. You may select to either not use them or implement
    them yourself.

    CURLOPT_WRITEDATA set to a objectpointer
    CURLOPT_INTERLEAVEDATA set to a objectpointer
    CURLOPT_WRITEFUNCTION set to a functionpointer
    CURLOPT_READDATA set to a objectpointer
    CURLOPT_READFUNCTION set to a functionpointer
    CURLOPT_SEEKDATA set to a objectpointer
    CURLOPT_SEEKFUNCTION set to a functionpointer
    CURLOPT_ERRORBUFFER set to a objectpointer
    CURLOPT_STDERR set to a objectpointer
    CURLOPT_DEBUGFUNCTION set to a functionpointer
    CURLOPT_DEBUGDATA set to a objectpointer
    CURLOPT_HEADERFUNCTION set to a functionpointer
    CURLOPT_HEADERDATA set to a objectpointer

    */

    ret = curl_easy_perform(hnd);

    curl_easy_cleanup(hnd);
    hnd = NULL;
    curl_slist_free_all(slist1);
    slist1 = NULL;

    return (int)ret;
    }
    /**** End of sample code ****/

Leave a Reply