{"id":450,"date":"2018-03-06T01:20:29","date_gmt":"2018-03-06T01:20:29","guid":{"rendered":"http:\/\/www.perisic.com\/blog\/sanfoh\/?p=450"},"modified":"2023-03-15T13:04:03","modified_gmt":"2023-03-15T13:04:03","slug":"links-used-in-using-curl-for-clarifai","status":"publish","type":"post","link":"https:\/\/www.perisic.com\/blog\/sanfoh\/2018\/03\/06\/links-used-in-using-curl-for-clarifai\/","title":{"rendered":"Using Clarifai via PHP and Curl"},"content":{"rendered":"<p><strong>Update March 2023:\u00a0<\/strong>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) &#8211; search for &#8216;curl&#8217;<\/p>\n<p><strong>Update:\u00a0<\/strong>link to Clarifai&#8217;s conference &#8211; Perceive 2020 &#8211; <a href=\"https:\/\/www.youtube.com\/playlist?list=PLs05ayZZ-2yg02sEAQ2Ffah2ilDYmc4Ch\">https:\/\/www.youtube.com\/playlist?list=PLs05ayZZ-2yg02sEAQ2Ffah2ilDYmc4Ch\u00a0<\/a><\/p>\n<p><strong>Update:<\/strong> I have now a project on github that is a cctv that uses Clarifai to identify concepts and make animated gifs:\u00a0<a href=\"https:\/\/github.com\/marcconrad\/myCCTV\">https:\/\/github.com\/marcconrad\/myCCTV<\/a><\/p>\n<p>php code, to find out what is in an image:<\/p>\n<p>$random_string=substr(str_shuffle(MD5(microtime())), 0, 5);<\/p>\n<p>echo &#8216;&lt;h1&gt;&#8217;.$random_string.'&lt;\/h1&gt;&#8217;;<\/p>\n<p>$imgurl=&#8221;http:\/\/perisic.com\/four\/train\/img.php?s=&#8221;.$random_string.&#8221;&amp;t=2.jpg&#8221;;<\/p>\n<p>$ch = curl_init();<\/p>\n<p>\/\/ set URL and other appropriate options<br \/>\ncurl_setopt($ch, CURLOPT_VERBOSE, &#8216;1&#8217;);<br \/>\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, &#8220;POST&#8221;);<br \/>\ncurl_setopt($ch, CURLOPT_URL, &#8220;https:\/\/api.clarifai.com\/v2\/models\/aaa03c23b3724a16a56b629203edc62c\/outputs&#8221;);<br \/>\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true); \/\/ curl_exec returns the value<\/p>\n<p>$headers = array(<br \/>\n&#8216;Content-Type: application\/json&#8217;,<br \/>\n&#8220;Authorization: xyzabcd&#8221;<br \/>\n);<br \/>\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);<\/p>\n<p>$fields = &#8216;{&#8220;inputs&#8221;:[{&#8220;data&#8221;:{&#8220;image&#8221;:{&#8220;url&#8221;:&#8221;&#8216;.$imgurl.'&#8221;}}}]}&#8217;; \/\/ Your image here.<br \/>\ncurl_setopt($ch, CURLOPT_POSTFIELDS, $fields);<\/p>\n<p>\/\/ grab URL and pass it to the browser<br \/>\n$result = curl_exec($ch);<\/p>\n<p>\/\/ echo &#8216;&lt;h2&gt;&#8217;.$result.'&lt;\/h2&gt;&#8217;;<\/p>\n<p>$mydata= json_decode($result, true);<\/p>\n<p>$concepts = $mydata[&#8220;outputs&#8221;][0][&#8220;data&#8221;][&#8220;concepts&#8221;];<\/p>\n<p>\/\/ var_dump($concepts);<\/p>\n<p>$conceptlist=&#8221;&#8221;;<br \/>\nfor($i=0;$i &lt; sizeof($concepts); $i++ ) {<br \/>\nif($i != 0 ) { $conceptlist .= &#8216;, &#8216;; }<br \/>\n$value = $concepts[$i];<br \/>\n$conceptlist .= $value[&#8220;name&#8221;];<br \/>\n}<\/p>\n<p>echo &#8220;&lt;h3&gt;Concepts: &#8220;;<br \/>\necho $conceptlist;<br \/>\necho &#8220;&lt;\/h3&gt;&#8221;;<\/p>\n<p>\/\/ close cURL resource, and free up system resources<br \/>\ncurl_close($ch);<\/p>\n<p>echo &#8216;&lt;img src=&#8221;&#8216;.$imgurl.'&#8221; alt=&#8221;&#8216;.$conceptlist.'&#8221; title=&#8221;&#8216;.$conceptlist.'&#8221;&gt;&#8217;;<\/p>\n<ul>\n<li>About the model: <a href=\"https:\/\/clarifai.com\/models\/general-image-recognition-model-aaa03c23b3724a16a56b629203edc62c#documentation\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/clarifai.com\/models\/general-image-recognition-model-aaa03c23b3724a16a56b629203edc62c#documentation<\/a><\/li>\n<li>How to POST JSON data with Curl from Terminal\/Commandline to Test Spring REST? <a href=\"https:\/\/stackoverflow.com\/questions\/7172784\/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/stackoverflow.com\/questions\/7172784\/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest<\/a> (for Windows: use &#8220;&#8221; to escape &#8220;)<\/li>\n<li>Download curl for Windows: <a href=\"https:\/\/curl.haxx.se\/download.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/curl.haxx.se\/download.html<\/a><\/li>\n<li>Or directly here: <a href=\"https:\/\/winampplugins.co.uk\/curl\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/winampplugins.co.uk\/curl\/<\/a><\/li>\n<li>&#8220;<code><code><span class=\"html\">After a lot of frustration with the fact that nobody has documented which curl commandline options go with which library functions, I discovered that the curl commandline will tell you (in the form of a C program) if you add `--libcurl foo.c`<\/span><\/code><\/code>If you&#8217;ve been struggling with trying to figure out how to get your fancy curl commandline to work in PHP, this makes it a breeze!&#8221; from: <a href=\"http:\/\/php.net\/manual\/en\/book.curl.php\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/php.net\/manual\/en\/book.curl.php<\/a><\/li>\n<li>About the &#8211;data switch and curl docu: <a href=\"https:\/\/curl.haxx.se\/docs\/httpscripting.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/curl.haxx.se\/docs\/httpscripting.html<\/a><\/li>\n<\/ul>\n<p><strong>Finally:<\/strong> C:\\Users\\vim\\curl\\curl.exe &#8211;verbose &#8211;libcurl tmp1.c -H &#8220;Authorization: Key xyzabcd&#8221; -H &#8220;Content-Type: application\/json&#8221; &#8211;data &#8220;{&#8220;&#8221;inputs&#8221;&#8221;:[{&#8220;&#8221;data&#8221;&#8221;:{&#8220;&#8221;image&#8221;&#8221;:{&#8220;&#8221;url&#8221;&#8221;:&#8221;&#8221;http:\/\/www.perisic.com\/five\/imgfive.php?s=helbo&amp;w=10&#8243;&#8221;}}}]}&#8221;\u00a0 https:\/\/api.clarifai.com\/v2\/models\/aaa03c23b3724a16a56b629203edc62c\/outputs<\/p>\n<p>(You must replace xyzabcd with your own API key)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update March 2023:\u00a0The 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) &#8211; search for &#8216;curl&#8217; Update:\u00a0link to Clarifai&#8217;s &hellip; <a href=\"https:\/\/www.perisic.com\/blog\/sanfoh\/2018\/03\/06\/links-used-in-using-curl-for-clarifai\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-450","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":5,"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/posts\/450\/revisions\/540"}],"wp:attachment":[{"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.perisic.com\/blog\/sanfoh\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}