Here is an example PHP function for the CloudSight API, given there are not many examples online.
Simply input your API key and the url which could be either a local or internet file, the function will return the string description of the image.
function cloudsightapi($key, $file_url) { $parse = parse_url($file_url); $size = getimagesize($file_url); $boundary = md5(time()); $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, "https://api.cloudsightapi.com/image_requests"); if (isset($parse['host'])) { $headers = array('Authorization: CloudSight '.$key, "Content-Type:multipart/form-data"); $postFields = array( 'image_request' => array( 'remote_image_url' => $file_url, 'locale' => 'en-US' ) ); $body = http_build_query($postFields); } else { $headers = array('Authorization: CloudSight '.$key, "Content-Type:multipart/form-data; boundary=".$boundary); $body = ''; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="image_request[locale]"'."\r\n\r\n"; $body .= "en-US"."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="image_request[image]"; filename="'.basename($file_url).'"'."\r\n"; $body .= 'Content-Type: '.$size['mime']."\r\n"; $body .= 'Content-Transfer-Encoding: multipart/form-data'."\r\n\r\n"; $body .= file_get_contents($file_url)."\r\n"; $body .= '--'.$boundary .'--'."\r\n\r\n"; } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); $ret = curl_exec($ch); $json = json_decode($ret, true); $token = $json['token']; $status = $json['status']; while ($status == 'not completed') { //grab the result curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_POSTFIELDS, null); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_URL, 'https://api.cloudsightapi.com/image_responses/'.$token); $ret = curl_exec($ch); $json = json_decode($ret, true); $token = $json['token']; $status = $json['status']; //print_R($json); sleep(1); } $label = $json['name']; curl_close($ch); return $label; }
Leave a Reply