Skip to content

Instantly share code, notes, and snippets.

@tineyedev
Created April 30, 2018 14:37
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 tineyedev/5bfe4f09a7aeeacd7ed9ea85ee450b47 to your computer and use it in GitHub Desktop.
Save tineyedev/5bfe4f09a7aeeacd7ed9ea85ee450b47 to your computer and use it in GitHub Desktop.
TinEye Commercial API image data search PHP code sample
<?php
function search_image($api_url, $api_private_key, $api_public_key, $image_data, $image_name) {
$http_verb = "POST";
// init CURL seesion
$handle = curl_init();
// content-type header
$boundary = "---------------------" . md5(mt_rand() . microtime());
$contenttype_header = "multipart/form-data; boundary=$boundary";
$date = time();
$nonce = uniqid();
$limit = "5";
$api_sig_raw = $api_private_key . $http_verb . $contenttype_header . $image_name . $date . $nonce . $api_url . "limit=$limit";
$api_sig = hash_hmac("sha256", $api_sig_raw, $api_private_key);
$header_boundary = $boundary;
$boundary = '--'.$boundary;
$post_str = $boundary . "\nContent-Disposition: form-data; name=\"image_upload\"; filename=\"$image_name\"\nContent_Type: application/octet-stream\n\n$image_data\n";
$post_str .= $boundary . "\nContent-Disposition: form_data; name=\"api_key\"\n\n$api_public_key\n";
$post_str .= $boundary . "\nContent-Disposition: form_data; name=\"date\"\n\n$date\n";
$post_str .= $boundary . "\nContent-Disposition: form_data; name=\"nonce\"\n\n$nonce\n";
$post_str .= $boundary . "\nContent-Disposition: form_data; name=\"api_sig\"\n\n$api_sig\n";
$post_str .= $boundary . "\nContent-Disposition: form_data; name=\"limit\"\n\n$limit\n";
$post_str .= $boundary . "--";
curl_setopt($handle, CURLOPT_URL, $api_url);
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Expect: 100-continue", "Content-Type: multipart/form-data; boundary=$header_boundary"));
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
// Call API and convert results to a usable JSON object.
$api_response = curl_exec($handle);
$api_json_response = json_decode($api_response, True);
if ($api_json_response['code'] == 200) {
print "status: Ok, ";
print "num results: " . $api_json_response['results']['total_results'];
} else {
print "status: Error, ";
}
curl_close($handle);
return $api_json_response;
}
// SANDBOX KEYS
$api_url = 'https://api.tineye.com/rest/search/';
$api_private_key = '6mm60lsCNIB,FwOWjJqA80QZHh9BMwc-ber4u=t^';
$api_public_key = 'LCkn,2K7osVwkX95K4Oy';
$image_filepath = '/home/dev/Pictures/meloncat.jpg';
$image_name = 'meloncat.jpg';
$image_data = file_get_contents($image_filepath);
var_dump(search_image($api_url, $api_private_key, $api_public_key, $image_data, $image_name));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment