您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13971|回复: 0
打印 上一主题 下一主题

[php学习资料] 图像识别API调用代码实例(PHP)

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-8 00:13:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。8 ~$ V1 m. P. ?( u0 t6 [( m
1.添加商品信息; `8 W2 m/ K' b+ ?% E5 ?+ l& x% i
2.添加图片
1 s  E6 W! Y/ R9 m7 ~- O1 ]2 n( X+ [3.查询所有产品
1 O' e; j3 t# F+ u& f8 Q: v4.查询某个具体产品信息. K. C; s5 y5 R2 n& G5 \
5.删除某个产品
$ |8 x: j3 j% T9 s3 @1 |6.删除图片
6 p, A" V( q5 ?3 H5 b" {7.图像识别7 `" V1 z/ n# `( ]$ l8 Q

( y: A0 u: R/ t2 w: J& j图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey
1 ^; m  S. M" \5 M# ^. r2 ]& c+ ~, z: }: U: f+ V
PHP示例:9 a. q0 V" x) T& H8 D* v
  1. <!--?php) M' ~+ q4 u% s3 m* G
  2. // +----------------------------------------------------------------------6 i3 j1 a5 ]! Z$ e, A$ [
  3. // | JuhePHP [ NO ZUO NO DIE ]
    ! y, y/ @6 C( s$ O
  4. // +----------------------------------------------------------------------
    : g/ y" I, Y5 m' k0 @. n2 A' r
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
    4 F6 d+ a, g: [, Y3 n+ i
  6. // +----------------------------------------------------------------------
    & u; x4 D+ w  M1 P
  7. // | Author: Juhedata <info@juhe.cn-->
    % s; o, n# X3 y( P  Y) o* C; |
  8. // +----------------------------------------------------------------------
    / h0 t6 b9 Q2 Q# ^' Z

  9. 5 N: I8 O# M; }
  10. //----------------------------------
    " t$ F1 p, _! e% _& o. ~6 {
  11. // 图像识别调用示例代码 - 聚合数据
    4 B. M* o7 X9 z
  12. // 在线接口文档:http://www.juhe.cn/docs/117$ }% _/ w4 h$ N$ F% W0 Z/ ~( U
  13. //----------------------------------
    7 V: E# r& i1 [, ]. w& W0 C

  14. 5 [; [& s( [* m; d4 b- R8 p+ e
  15. header('Content-type:text/html;charset=utf-8');
    5 m# Z" o5 F: W+ C. P
  16. % B6 p; T* I2 X6 B

  17. 5 ?& _; v+ }4 p: [: U5 a
  18. //配置您申请的appkey  w1 z" E, A. s+ Q
  19. $appkey = "*********************";& ~6 G3 K& j$ G- F9 U5 q) {* N

  20. ; S: Z4 R$ N. y! v7 w7 j- a3 y

  21. 6 N" C6 Z0 k! j- O! u! [1 o& |
  22. * C) ?$ M4 d* }/ Q& t3 _
  23. 2 f3 r" Q7 w# Z, ^
  24. //************1.图像上传************
    ) ~% J9 e; r! |  T
  25. $url = "http://japi.juhe.cn/image_recognition/upload";
    ( S+ l* U. X3 f6 ]" l5 j
  26. $params = array(
    5 N$ |) i) W0 ~
  27.       "file" => "",//上传的单张图片
    7 D$ j# M, G9 w/ m8 B, y5 l) s
  28.       "key" => $appkey,//APP Key
    7 T8 W0 W3 L2 O( ^: @. O$ [
  29.       "pname" => "",//包名
    5 E/ _, c1 ^9 n0 j
  30. );
    $ y9 n/ C/ w8 A" C
  31. $paramstring = http_build_query($params);) z, O( Y! E! W, B" q# O' n# O: i7 C
  32. $content = juhecurl($url,$paramstring,1);
    3 I  k+ ?6 n4 g/ K. a  d5 b8 R
  33. $result = json_decode($content,true);
    / N. h/ z8 i3 q0 J3 N
  34. if($result){/ m. A6 O: o  I7 ]+ {( v. \) }
  35.     if($result['error_code']=='0'){$ S9 }6 N9 Z$ A' h" b  e
  36.         print_r($result);6 R8 T  f4 h& U. X
  37.     }else{1 N4 q3 n5 M% g( r' D
  38.         echo $result['error_code'].":".$result['reason'];
    $ O) H: F9 a0 l7 c) u6 ~  y
  39.     }
    # v# \# v( s6 J9 n# c/ e
  40. }else{
    $ Y! a7 J7 D, s# W$ O( U2 o& A' |
  41.     echo "请求失败";
    6 ^( u5 Q% V/ h6 Y8 [! l: ^
  42. }  J0 A# l  ^/ j! P
  43. //**************************************************: k! B, h. t: w, O3 I; F

  44. / n0 {+ ^& ^' e7 T  T( f

  45. : t, S, L# T  }8 g$ T

  46. 3 B$ _$ E9 o/ F, I
  47. 3 s5 r& Y: S& s% J; }6 v
  48. //************2.查寻上传图像信息************
    9 F$ C0 s8 w  U/ x3 a
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";( U% r6 k: Y* a" A. q
  50. $params = array(
    % L# f$ Q3 ^: R1 P. \, g7 J
  51.       "key" => $appkey,//APP Key
    $ c7 [% F$ c; h! S- c
  52. );
    4 s2 E. p9 E# n/ l% [  S
  53. $paramstring = http_build_query($params);4 h: U6 Z* |+ ?; }$ T, k+ s
  54. $content = juhecurl($url,$paramstring);$ {, c9 x& v( B! R, W
  55. $result = json_decode($content,true);
    ( j1 o. |* y& T8 o# `9 \' V" u
  56. if($result){
    % J# }4 t3 Q( O4 `
  57.     if($result['error_code']=='0'){
    ' @  Z9 P6 r* N* f' L+ f
  58.         print_r($result);
    * L3 W$ m: b7 B: A0 ~' N/ i' {; f
  59.     }else{; i9 k& ^" L( H( X+ C
  60.         echo $result['error_code'].":".$result['reason'];
    6 {1 g# C& i" [& Z
  61.     }. o0 @8 P- Y" r3 L5 J+ `, A
  62. }else{! \% n' s4 t: B
  63.     echo "请求失败";
    . l7 T: \2 `3 o1 u
  64. }$ \: {' X9 }7 o, n
  65. //**************************************************
    9 x* F! q: y# |( j, F, e
  66. + k; i! F% [% g8 D3 d
  67. 4 Q( @. M* s4 w9 j6 F
  68. ' g& q( _$ b9 A

  69. ( j. n$ r" K* ~
  70. //************3.删除图像信息************
    6 U- J5 ?. j/ E( k" w2 D
  71. $url = "http://japi.juhe.cn/image_recognition/delete";3 V9 K8 l3 M8 g
  72. $params = array(0 t! r8 p4 I" P
  73.       "key" => $appkey,//APP Key6 o7 [- k9 k) T! M# G/ B- y8 \" a
  74.       "imageId" => "",//图片ID
    4 n" E7 L8 I" u. U
  75. );
    9 E* ^6 |5 e3 K- N  m
  76. $paramstring = http_build_query($params);
    ! O8 b2 y3 I5 z
  77. $content = juhecurl($url,$paramstring);- G) t9 `: N1 Z% Y* L
  78. $result = json_decode($content,true);2 u1 `- H) [$ X# V% g2 ~4 @+ n/ @
  79. if($result){' y2 ?9 k& o  b2 O3 y: y: Z: o$ S
  80.     if($result['error_code']=='0'){
    + c" a* ?; D& O2 Z
  81.         print_r($result);
    3 C8 }* Q) A% T: y) N+ V+ r# B7 S
  82.     }else{
    ! v& O2 K6 [3 D- _9 Q
  83.         echo $result['error_code'].":".$result['reason'];
    7 ]& L& ^3 C5 k2 M% f$ D" i) o- W
  84.     }
    % x+ ?  \! q! i5 G; z7 Q3 [9 j
  85. }else{9 C9 ^# ]$ z9 v. |$ L+ O
  86.     echo "请求失败";! Y$ F8 d! |3 P) Z; Z6 O. G  d9 M
  87. }
    & |) S, j  I  J1 P, U4 m
  88. //**************************************************. _. `. Z3 s+ W% f

  89. 2 q. n$ f  g4 q, r/ d$ z8 |. Q
  90. $ G: u, g  X$ O8 V' t
  91. # Z' T8 L$ ]8 a/ w

  92. $ c5 r' p7 I) k: j2 d
  93. //************4.图像识别************
    0 R- {: h) |- J2 \4 V
  94. $url = "http://japi.juhe.cn/image_recognition/check";$ s. W1 ?3 O- |
  95. $params = array(
    9 r$ H  G  X6 u
  96.       "file" => "",//上传的图片; C3 ?1 `' \- R$ F, F
  97.       "key" => $appkey,//APP Key2 g5 b  e) {/ O: @4 m
  98.       "pname" => "",//包名
      E  h& ~) p  p6 l
  99.       "device_name" => "",//设备名称(如android OS或iphone OS)
    ; z# Y; T' k% C# H4 y; b" Z' x" }
  100.       "device_version" => "",//设备版本(如:4.0.3)2 s. n; b, N. O
  101.       "latitude" => "",//纬度
    ; \5 b/ y5 z! E
  102.       "longitude" => "",//经度
    : o; u/ T1 ?. d5 Q
  103.       "uuid" => "",//设备id
    5 _& j" U/ y( K* ^1 n
  104.       "zone" => "",//请求地区  o) Q! y0 Z6 p# S
  105. );
    7 x2 q& T( Z1 N: f# O5 `' @3 B
  106. $paramstring = http_build_query($params);
    ) v, L' ~, o2 u$ |5 M& G
  107. $content = juhecurl($url,$paramstring,1);0 L/ `7 ]2 B6 K5 R8 u
  108. $result = json_decode($content,true);
    ' h1 d" }! D) @2 @8 x+ `" i: P0 w
  109. if($result){
    / h$ g7 k6 E' f" ^. |+ w
  110.     if($result['error_code']=='0'){
    6 e2 g7 ^/ y: K2 ^# M' n; D3 t+ ^
  111.         print_r($result);
    + r1 B- O9 S) {8 E8 A' s- G
  112.     }else{: x4 Y+ n; E) P0 D+ ^
  113.         echo $result['error_code'].":".$result['reason'];
    ! }& @' _6 g9 e/ J8 i: x
  114.     }
      b9 K+ A+ P9 @/ u+ W. H" n
  115. }else{& f  z! s& B. m; G2 E9 E
  116.     echo "请求失败";" h8 y8 m$ k! a8 h
  117. }
    . N- t& l  S, N0 K" M  L7 Q
  118. //**************************************************
    , N4 o. Z! d6 \4 t6 J# Q+ o8 W8 n  l

  119. 8 T7 \- }3 w  T$ T6 Y0 ^5 N$ i

  120. . H" n% T/ X& j, F3 e+ h

  121. " H8 S2 z1 D  o' S  X8 m) k
  122. , c: O+ C9 D+ w) m
  123. 3 ]' a7 s6 g: Z& a
  124. /**
    ; Q% a9 a$ Q0 h% l* \( v
  125. * 请求接口返回内容
    + ~4 ]" r1 ]% ^8 k
  126. * @param  string $url [请求的URL地址]5 Z+ u# i7 {4 L- F( `# w6 Q9 r2 p
  127. * @param  string $params [请求的参数]
    ( D/ i1 ?+ }4 M" z
  128. * @param  int $ipost [是否采用POST形式]
    . l  n7 B- t4 Z0 f! k  ]3 r, _4 q
  129. * @return  string" Q+ y5 p+ s- _. P0 l# B/ {
  130. */
    : W' R! E3 `  `
  131. function juhecurl($url,$params=false,$ispost=0){
    ' u1 }$ D+ z6 T0 l; Q. z
  132.     $httpInfo = array();6 Y% x! r! ]6 u: W
  133.     $ch = curl_init();% h6 @, c# p- p* C4 n- J6 R6 u
  134. 9 \" z' N/ p! y6 Q
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    ) u9 e  F* _; y5 B1 O% w; T
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    0 L+ d  a4 M; E5 |
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );4 Y% r6 X+ k, ^1 V3 W1 |
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    , R3 i2 W' t+ v
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    * v8 |, u9 M' P% T
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    : [4 }- E1 ]; v! h
  141.     if( $ispost )
    6 N) o4 K" `! {% Z# ~
  142.     {- o/ W3 h$ P# s  P! {
  143.         curl_setopt( $ch , CURLOPT_POST , true );7 O* b4 K( \7 z+ e8 i
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
    & K4 T, w7 {( O4 [; a" }: y3 t
  145.         curl_setopt( $ch , CURLOPT_URL , $url );0 @: M" u! Q. S+ h  v' h' w9 t" B# l
  146.     }
    1 }0 t# b& a; h! U0 g
  147.     else& ^. ]) |( O+ q) v. T
  148.     {( p, K. X  V5 b: A% T
  149.         if($params){
    5 h; E! ]( p# F2 R$ t5 t" I
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );& {, v3 x: p0 A6 [
  151.         }else{* U$ k- K! K* e) ]# o
  152.             curl_setopt( $ch , CURLOPT_URL , $url);9 r3 c4 J' B% T2 t
  153.         }
    0 A6 k7 g4 E/ a) @& f) _; B* S- O7 Q4 \
  154.     }
    ! j3 D+ q0 A5 L% D
  155.     $response = curl_exec( $ch );- f7 W7 F4 W; ^1 R0 N
  156.     if ($response === FALSE) {
    ( H$ a* V' q% e# |% `
  157.         //echo "cURL Error: " . curl_error($ch);
    % E5 A, S: x3 n& a9 |
  158.         return false;+ a( R0 n2 g# x2 P
  159.     }; G# Z$ v) h* Q( F: W' D4 E: b
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );! X$ p' s+ F& t
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    , N9 c6 R4 w1 p' H5 K
  162.     curl_close( $ch );# {! ]6 h" [" I7 F$ g/ S8 W; P
  163.     return $response;* U$ a) Q2 i/ g% I+ D. Q" d6 q
  164. }
复制代码
6 E0 b5 L2 O; Y& \; I! x0 `$ G
% u7 w( i! G2 C5 M/ l' ~2 m. y
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-5-1 02:10 , Processed in 0.062579 second(s), 21 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!