cncml手绘网

标题: 图像识别API调用代码实例(PHP) [打印本页]

作者: admin    时间: 2018-7-8 00:13
标题: 图像识别API调用代码实例(PHP)
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。: C; v/ G  b$ p6 D0 l
1.添加商品信息
0 g7 q3 J9 j/ Q9 |! V2.添加图片
( W9 S4 `/ i' b  w7 \# f: s3.查询所有产品
6 s4 h" B. e! k: D7 R4 |; W4.查询某个具体产品信息( [3 ~0 ]- T4 F
5.删除某个产品; c; O- O! Z  j3 L. q/ A
6.删除图片( Z  z, T* }% R. C0 `$ N
7.图像识别
3 K5 O" X# I  c' t- C
6 T% a5 \% y6 X/ Y/ S' |5 W图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey
, p$ W1 H5 b# N: C: f- }1 U) Q1 R2 c- p; T3 F; }3 Q
PHP示例:) l2 q0 N' r  Z) }  Z
  1. <!--?php
    6 L* C& a$ ?" Y2 C/ v# F( K
  2. // +----------------------------------------------------------------------# U, q3 \! D" f$ w, e; L4 X
  3. // | JuhePHP [ NO ZUO NO DIE ]
      q1 p/ d$ j! J* T/ K- C
  4. // +----------------------------------------------------------------------
    " T6 e  G) {1 }4 T; K1 L' w: g
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
    ( o4 o0 Q9 `. [6 ?7 C  \
  6. // +----------------------------------------------------------------------
    5 f: J# r3 h6 l  D; v" C" t
  7. // | Author: Juhedata <info@juhe.cn-->% ~# G7 K( {$ t: F6 {0 s- z8 ]
  8. // +----------------------------------------------------------------------
    ( W7 w! ~% h1 X
  9. & w7 S: t7 H) j6 w& W' u; m
  10. //----------------------------------
    ( m0 M' Z0 P. b2 j* S
  11. // 图像识别调用示例代码 - 聚合数据
    7 U3 F/ S- r" s7 N% M
  12. // 在线接口文档:http://www.juhe.cn/docs/117$ Z7 Y: j" ?  m2 Y9 g# R  w
  13. //----------------------------------
    6 c# n/ B; ?% T5 i; H- @! J
  14. : N" r; p0 |  d7 M" I! j& D
  15. header('Content-type:text/html;charset=utf-8');) V/ R( Y% B" t+ F0 b- C3 Y! Y

  16. 2 w' g6 P, M8 P- I& E

  17. : s9 u. M. Q) J/ ]4 j5 X0 Z; D' B
  18. //配置您申请的appkey! K5 v; _% ~! o9 {  I, x; N
  19. $appkey = "*********************";$ t$ J$ O4 b/ T8 Y2 A
  20. ' ?" W: Y8 K; `* `* @
  21. ( \4 l0 S& D1 o

  22. : {  p7 p4 `" T6 ~- i' X+ N

  23.   T5 B  Z4 K, b
  24. //************1.图像上传************9 |: N# z5 U3 m9 C# _
  25. $url = "http://japi.juhe.cn/image_recognition/upload";: d4 p# V8 {# W
  26. $params = array(
    5 t5 D$ g3 o9 B4 l; Y- W
  27.       "file" => "",//上传的单张图片4 j; [2 F3 a; ?; x4 W$ N5 f4 A
  28.       "key" => $appkey,//APP Key
    3 n, v$ C+ {7 K( M% E- m) {$ M: @% u% W
  29.       "pname" => "",//包名
    3 r' _+ r% [* l! q' S" F
  30. );/ o" {6 C% A: e2 U9 k
  31. $paramstring = http_build_query($params);/ u7 w  q  b6 {2 J# o3 q
  32. $content = juhecurl($url,$paramstring,1);& v; l1 O3 a" _7 ?0 A
  33. $result = json_decode($content,true);2 d5 B7 f2 Z) W. ~
  34. if($result){$ v' x2 }, M5 r9 R1 t+ u
  35.     if($result['error_code']=='0'){
    , y9 T- c6 u& H0 s
  36.         print_r($result);
    9 t, {" K, ]5 b8 h: L- v
  37.     }else{
      x/ w7 m+ }: ?/ h1 `
  38.         echo $result['error_code'].":".$result['reason'];
    $ M( B9 W; z) j9 A2 |/ h' l$ s) l
  39.     }2 [  U! P7 T( h8 O% |3 h
  40. }else{
    ( k/ g" b6 ~6 v+ ^
  41.     echo "请求失败";+ B3 q' }/ u* Y6 l
  42. }3 I6 F+ M7 J! s# {8 a3 g9 _
  43. //**************************************************" v) [8 n! J8 s0 z9 S) u' V2 e
  44. . G3 P- ^* c3 \" P& b8 N

  45. ! t+ A3 |8 _; u

  46. + u% ~  l: O  B( a' g7 ]. }
  47. $ \" s; C4 K4 G+ _7 }# ]& Z; ]
  48. //************2.查寻上传图像信息************* u4 S6 T' G/ ^0 L+ t6 A
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";
    % c' N$ C9 R3 {3 w5 S
  50. $params = array(4 a3 n" [" _' B' h; r, K" b$ o. [5 c9 F
  51.       "key" => $appkey,//APP Key' O' _& W3 {+ G5 y' n0 q' J
  52. );1 l+ a) ?* @! b/ N) I
  53. $paramstring = http_build_query($params);
      p! U$ h1 C8 _* c. N+ o5 y4 W
  54. $content = juhecurl($url,$paramstring);! W+ S& G# G" I
  55. $result = json_decode($content,true);
    9 y4 Y! h: S% N' ^0 r1 c1 B
  56. if($result){+ ]! k5 k: Z0 P: S# x
  57.     if($result['error_code']=='0'){
    8 a( ?8 Y- ^- s5 i5 T2 g
  58.         print_r($result);
    : @/ v% P; l& c/ X: y" s
  59.     }else{# x' j! O5 d% q$ ~. S' E
  60.         echo $result['error_code'].":".$result['reason'];. S% W- n/ z! r7 t* a+ F8 M
  61.     }% B7 R! q/ r2 T/ Y
  62. }else{
    0 B4 S% c7 p2 x. |) U7 W. B) G
  63.     echo "请求失败";
    # D& _1 y) S. @7 |" [6 l- G
  64. }/ J, K& q* r% L; c, U3 n* S
  65. //**************************************************
    0 S: F/ |0 F. A- @' H9 @$ _5 }' [& `
  66.   t/ V' U. L5 N$ ]6 Q

  67. / E% {0 U# E2 L5 l
  68. - W9 k- g" X: A2 A& P9 }- A, V3 p

  69.   Y8 [4 e+ X4 n2 P' X# }
  70. //************3.删除图像信息************  P  g0 @- L$ G" l
  71. $url = "http://japi.juhe.cn/image_recognition/delete";7 i1 U5 o7 c! E/ m" u0 [
  72. $params = array(- N% ?, S6 Y% m& O8 j
  73.       "key" => $appkey,//APP Key
    - J$ i, P4 ?& T2 f* S* O: t
  74.       "imageId" => "",//图片ID
    . m$ J# _9 E1 F
  75. );
    - F# h! h: V& a/ }' H
  76. $paramstring = http_build_query($params);+ w$ z: r/ y4 v3 V) m
  77. $content = juhecurl($url,$paramstring);1 t+ B: g; N# m8 g( y) r
  78. $result = json_decode($content,true);! g+ U9 W8 i  F/ r
  79. if($result){
    4 @1 u( o$ c! d# `( ~, I
  80.     if($result['error_code']=='0'){: O4 n( M' W; D$ ^' [, m
  81.         print_r($result);
    % k6 Y! l2 Y0 }0 f
  82.     }else{+ _9 t; Q5 a  T# o. H* P
  83.         echo $result['error_code'].":".$result['reason'];
    . T( m' z7 m- q/ [9 U/ ^, [
  84.     }  N1 O/ _* I' I# y
  85. }else{
    - |" c) }6 m" o
  86.     echo "请求失败";
    . n( E0 N- u8 C$ C5 o
  87. }
    ( k* g9 N8 w" [& M, O  Q, l
  88. //**************************************************
    , s0 H/ R+ m3 j+ C& x
  89. 8 B2 E' {- w  D2 G
  90. 2 C# N$ j: h( F/ j

  91. 6 R# Y/ s7 s- E5 I) ?0 R+ A6 [
  92. & A1 k' a, T8 E( f/ Y
  93. //************4.图像识别************9 b3 P" a9 M  z9 a: r
  94. $url = "http://japi.juhe.cn/image_recognition/check";
    # Y5 p; F( Y+ g
  95. $params = array(
    ) M3 D4 `# ~; m4 z% a
  96.       "file" => "",//上传的图片* }5 `: P' @$ O
  97.       "key" => $appkey,//APP Key9 f  p* A6 x! @# E
  98.       "pname" => "",//包名, ]1 M, w! N# g) ]( r
  99.       "device_name" => "",//设备名称(如android OS或iphone OS)' @/ l* @$ t1 C% d4 R
  100.       "device_version" => "",//设备版本(如:4.0.3)
    0 X7 B0 L9 z9 c' m
  101.       "latitude" => "",//纬度3 P& b& S7 O0 }, l# n$ {
  102.       "longitude" => "",//经度
    ) o8 H6 W: ~* A. s% B
  103.       "uuid" => "",//设备id3 }, ^6 i7 E5 {3 C8 V8 M/ J. D4 h
  104.       "zone" => "",//请求地区8 ~+ |% m; U) _/ j, I
  105. );- u# G8 h* m. T7 e5 T: }* x
  106. $paramstring = http_build_query($params);
    - [+ H) B* K6 d( r! v& r8 D) J# T
  107. $content = juhecurl($url,$paramstring,1);
    + R: z1 d% p7 K$ h0 M0 D$ ~3 o
  108. $result = json_decode($content,true);
    + {5 a. ]- p9 E* x
  109. if($result){; P+ i1 C- ^+ Q1 l$ T9 M
  110.     if($result['error_code']=='0'){; K" `; L( o$ A1 V
  111.         print_r($result);- }2 I8 A. A! `* I: Q. K
  112.     }else{
    9 B( h' ?8 w0 \) X' E2 K# o' q
  113.         echo $result['error_code'].":".$result['reason'];& G  e) y5 p! n9 K/ c( G
  114.     }
    3 `9 U# h% L2 M$ y
  115. }else{3 S, p5 c7 E4 o8 g
  116.     echo "请求失败";$ k7 R$ b1 x- ]0 [. M
  117. }6 _* L: M( B4 t* o0 K
  118. //**************************************************
    2 l3 z1 |; s/ y! y0 P1 H
  119. 5 b2 [$ o& z, X1 N
  120. - ^$ v8 C. V. e5 t5 F% x

  121. , C! {+ t; k' e& Q7 r

  122. 6 v- a% O; |' F' m
  123. $ n7 F/ O$ n" Z4 B& k( b
  124. /**
    - O  g0 W6 D* f# s. d
  125. * 请求接口返回内容  l' W' H$ q9 H+ n  l
  126. * @param  string $url [请求的URL地址]
    ; k/ o- G1 O) s# z. s* l
  127. * @param  string $params [请求的参数]5 l7 y" T5 u7 b3 g! g6 @" N. x
  128. * @param  int $ipost [是否采用POST形式]
    ( a/ u1 J: L( V: O
  129. * @return  string
    # Z% s5 z* x3 o4 G2 k8 K3 N
  130. */  L- \; T/ n4 [: Z+ c8 ~
  131. function juhecurl($url,$params=false,$ispost=0){
    2 I. K/ k, {$ `$ C- l# W
  132.     $httpInfo = array();; n+ ?: G# o1 b# ?' K
  133.     $ch = curl_init();. M, R3 R8 J1 Z5 v; B

  134. 2 d" f9 K: V0 w
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );5 r( K0 T! w6 z
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );- v" J: \( e; Q- K  F7 j
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    : D. C6 x5 B2 f! g5 m
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);( U5 l5 S4 {: X
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );6 F& v  L4 m# _' e. v& D
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);* V' F7 [4 i) Q2 J. Y9 A) P
  141.     if( $ispost )  M# Q: D) E7 _  ?
  142.     {  b5 I$ o& s+ H+ N( ^+ z* w
  143.         curl_setopt( $ch , CURLOPT_POST , true );/ S0 g6 s1 a) g
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );1 I" T; n6 K! X, i3 [, F/ g
  145.         curl_setopt( $ch , CURLOPT_URL , $url );
    - J+ {8 L8 r8 Z" k
  146.     }; y) i7 H. r. ~- h( p( E) B
  147.     else7 t* ~. X0 h/ s4 t
  148.     {0 g+ W  Q' a9 J3 E4 `4 P+ t
  149.         if($params){
    " |) \6 P. n' J. a( \
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
    7 s9 M( l! s5 E7 q
  151.         }else{
    2 w1 f* O. y; z0 T4 v- K+ Z2 i# G
  152.             curl_setopt( $ch , CURLOPT_URL , $url);
    # Y1 i' m# T; x* d4 F( T4 g
  153.         }& ~. l0 k7 o3 ^* G( m- a1 U; n* H
  154.     }
    0 `& |* f; k  d3 y
  155.     $response = curl_exec( $ch );
    . c0 ]" P- Y2 r' k5 [
  156.     if ($response === FALSE) {
    , q, n  |1 O8 A- V( }
  157.         //echo "cURL Error: " . curl_error($ch);
    0 o  |6 O; N7 F4 Y4 i+ L  h
  158.         return false;$ w. N# r7 L+ I2 ~9 n! [/ E
  159.     }# o  c" s  r3 I1 X& ]8 |
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    . w9 j+ p1 y2 `: m* m9 j
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    , K4 T! m9 l1 V
  162.     curl_close( $ch );5 Y8 g3 G& s. N: p& K% [  U; O
  163.     return $response;3 ]  S, q6 s* x5 d& z
  164. }
复制代码

' r  X# l. z9 q0 b8 `5 a
  {# e% e- g, A! s" ]




欢迎光临 cncml手绘网 (http://bbs.cncml.com/) Powered by Discuz! X3.2