recognize.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. //云图库设置
  3. define('CLOUDKEY', '<这里是云图库的Cloud Key>');
  4. define('CLOUDSECRET', '<这里是云图库的Cloud Secret>');
  5. define('CLOUDURL', 'http://<这里是云图库的Client-end URL>/search');
  6. header('Content-Type: application/javascript; charset=UTF-8');
  7. // step 1: 获取浏览器上传的图片数据
  8. $image = getHttpData();
  9. if (!$image) showMsg(1, '未发送图片数据');
  10. // step 2: 将图片数据发送云识别服务
  11. $params = array(
  12. 'timestamp' => time() * 1000,
  13. 'appKey' => CLOUDKEY,
  14. 'image' => $image,
  15. );
  16. $params['signature'] = getSign($params, CLOUDSECRET);
  17. $str = httpPost(CLOUDURL, json_encode($params));
  18. if (!$str) showMsg(2, '网络错误');
  19. // step 3: 解析识别结果,返回给浏览器使用
  20. $obj = json_decode($str);
  21. if (!$obj || (isset($obj->status) && $obj->status == 500)) {
  22. showMsg(2, '网络错误');
  23. } else if ($obj->statusCode != 0) {
  24. showMsg(3, '未识别到目标');
  25. } else {
  26. showMsg(0, $obj->result->target);
  27. }
  28. /**
  29. * 获取浏览器上传的图上数据
  30. * @return string
  31. */
  32. function getHttpData() {
  33. $image = getPostImage();
  34. if (!$image) $image = getPostFile();
  35. return $image;
  36. }
  37. /**
  38. * WebAR使用
  39. * @return bool|string
  40. */
  41. function getPostImage() {
  42. $data = @file_get_contents('php://input');
  43. if ($data) {
  44. $obj = json_decode($data);
  45. $data = $obj->image;
  46. }
  47. return $data;
  48. }
  49. /**
  50. * 微信小程序使用(上传文件处理)
  51. * @return string
  52. */
  53. function getPostFile() {
  54. $data = '';
  55. if (isset($_FILES)) {
  56. foreach ($_FILES as $file) {
  57. if ($file['error'] == 0) {
  58. $data = base64_encode(@file_get_contents($file['tmp_name']));
  59. break;
  60. }
  61. }
  62. }
  63. return $data;
  64. }
  65. /**
  66. * 生成签名,使用sha256加密
  67. * @param $params
  68. * @param $cloudSecret
  69. * @return string
  70. */
  71. function getSign($params, $cloudSecret) {
  72. //按字典顺序排序
  73. ksort($params);
  74. $tmp = array();
  75. foreach ($params as $key => $value) {
  76. $tmp[] = $key . $value;
  77. }
  78. $str = implode('', $tmp);
  79. return hash('sha256', $str . $cloudSecret);
  80. }
  81. function showMsg($code, $msg) {
  82. $arr = array(
  83. 'statusCode' => $code,
  84. 'result' => $msg,
  85. );
  86. echo json_encode($arr);
  87. exit;
  88. }
  89. function httpPost($url, $data) {
  90. $ch = curl_init();
  91. curl_setopt($ch, CURLOPT_URL, $url);
  92. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  93. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  94. 'Content-Type: application/json; charset=utf-8',
  95. 'Content-Length: ' . strlen($data)));
  96. curl_setopt($ch, CURLOPT_POST, 1);
  97. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  98. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  99. $str = curl_exec($ch);
  100. curl_close($ch);
  101. return $str;
  102. }