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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-8 00:13:40 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。/ i5 W9 ?; M: {0 {8 r1 W+ |
1.添加商品信息
2 y# K& n' ^2 b# a0 P: H) m2.添加图片
" U7 C( P* b4 C) k# N; a3.查询所有产品
% l6 G, p- d4 _9 Z0 s" S4.查询某个具体产品信息
2 [& }0 F, P: r1 D% x7 H5.删除某个产品
6 D# ]2 Q. A, P1 x, H3 R3 M4 J6.删除图片
9 P5 S" g3 K9 V5 |7.图像识别1 V+ [8 T# n# d7 @4 g

& H1 A$ \  }: A# a- K) U图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey; K4 H4 |- V0 q1 O8 f

" E* ]4 `, D; LPHP示例:
3 E3 `7 i/ V. e4 a7 q, ~( e) v
  1. <!--?php
    ) R/ r" S$ V1 X7 [5 X$ q
  2. // +----------------------------------------------------------------------
    # I% f1 |7 _6 W% E& N' {
  3. // | JuhePHP [ NO ZUO NO DIE ]" \; w6 t2 J. D  c) `7 c
  4. // +----------------------------------------------------------------------
    ( j' [% H' D. Q# t3 w; d) V3 E, f
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
    $ h& l! ?% i4 a8 n# o: U" g
  6. // +----------------------------------------------------------------------
    0 C8 ^  B! ^  j' q- J
  7. // | Author: Juhedata <info@juhe.cn-->
    0 |5 `% [; d& z0 a
  8. // +----------------------------------------------------------------------6 B, |1 D) r  l
  9. * `* L3 |6 _8 x+ `
  10. //----------------------------------
    - |) G7 j/ _6 n9 V7 J9 T
  11. // 图像识别调用示例代码 - 聚合数据
    $ C  J& Y& |) i1 g5 O% u  W* }6 @
  12. // 在线接口文档:http://www.juhe.cn/docs/117- G( R7 H9 c6 O" N
  13. //----------------------------------; M- M/ C, r& M$ H8 g/ h7 Y

  14. 8 P0 W% j0 U$ x3 F( i5 H/ G! G
  15. header('Content-type:text/html;charset=utf-8');! K8 @, O$ H) E4 C3 c, T" W& m

  16. 6 s9 U" P: P2 g9 R. j
  17. : w& D5 Y( D% t4 s
  18. //配置您申请的appkey
      C5 F0 a8 r: K
  19. $appkey = "*********************";
    ' A* {& j2 f+ j# ^6 n/ P+ n
  20. ( Z* d# h! O3 i. v0 [
  21. ) }1 e4 F0 s) ^% z
  22. 1 L& j; P9 ?0 m6 w( v
  23. % F! u4 x6 H- \/ [# V2 f% l/ [- u! a
  24. //************1.图像上传************
    ( O/ C9 k: [: K" j5 A6 \' x4 p
  25. $url = "http://japi.juhe.cn/image_recognition/upload";
    & A# J4 o8 Q3 m) M
  26. $params = array(- B# T8 r, O- d. ?
  27.       "file" => "",//上传的单张图片+ T# Q) L1 j8 J2 L7 c) n* w9 G
  28.       "key" => $appkey,//APP Key
    , Q' Y' f. J. V0 g& E: h2 p
  29.       "pname" => "",//包名- V4 d7 F( }+ M
  30. );! @( k7 T4 z) n, G7 \8 y
  31. $paramstring = http_build_query($params);5 I4 w  X  t! J4 @/ D) C
  32. $content = juhecurl($url,$paramstring,1);/ K; X# g( n: O
  33. $result = json_decode($content,true);/ E, P9 o' `- I
  34. if($result){  L4 d7 i9 Z$ t+ E9 `
  35.     if($result['error_code']=='0'){
    : h/ z! }2 `- c: L; H' L% ~7 {
  36.         print_r($result);
    % b& M' q1 y# Z8 W4 V
  37.     }else{9 i/ Y) p5 y& c3 u. G3 V
  38.         echo $result['error_code'].":".$result['reason'];
    8 g0 Y7 T8 Y$ ^7 R" U2 A
  39.     }
    4 [" u! O3 ^' x& ]! I, Q4 p2 d
  40. }else{
    3 o5 A4 _- h9 w( A+ D# H. i& r
  41.     echo "请求失败";2 J6 q6 [* K+ A
  42. }6 R& z& W" n3 M6 E2 H6 J& A1 p
  43. //**************************************************
    - J$ y' k# E9 Z5 c# g& O
  44. : ?' p; ^: c9 J3 {3 ?8 Y2 _+ ]1 A/ _! q

  45. ! G& ]2 V! Q5 W" Y: w% n
  46. , v8 Z9 _+ ]$ }* ^3 K: u1 I( v# @

  47. , D; t3 s* Q  [9 k* T  R4 f6 i
  48. //************2.查寻上传图像信息************
    ( e2 L# P8 O  H# b( p8 u
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";$ g/ \& {# l* o" y( D: D
  50. $params = array(7 B2 v* f1 l$ t. _. g9 ?  t6 y% [
  51.       "key" => $appkey,//APP Key& N  x- y2 B# z$ s
  52. );
    " \, X; U# b0 o* z0 \( I0 Y+ ^
  53. $paramstring = http_build_query($params);
    6 e5 P1 E. |) M  F* a2 l- M
  54. $content = juhecurl($url,$paramstring);
    ( x, a1 o  M2 O. G
  55. $result = json_decode($content,true);% Z$ a- M2 i' H! u* t1 H# {
  56. if($result){
    7 G1 \/ x$ c) v( L7 T
  57.     if($result['error_code']=='0'){7 p5 e) @/ H) _) f+ n, a
  58.         print_r($result);/ I2 N5 _/ g' p. Y1 q6 y2 H3 Y
  59.     }else{
    . \4 }9 W9 D; e0 V5 m  T$ j
  60.         echo $result['error_code'].":".$result['reason'];
    - Z; j+ y1 @& H+ [
  61.     }
    5 u" `/ Z5 c$ V2 n- R! t9 }
  62. }else{
    4 J& s4 E4 ^2 M1 P2 a
  63.     echo "请求失败";
    ( |! k7 P% V: p/ y; ?* C! K$ i
  64. }( Y( r9 c' u$ I" q& _+ m2 g
  65. //**************************************************
    8 n( ?( R* z" o! c8 C  b+ z) Q
  66. + U( N4 M  q3 Y( ?
  67. - P) B0 a+ C7 Q8 e
  68. ( X! o5 E9 Q( t2 p- j# @6 Y! ]% A
  69. / x8 Y& T! @1 g/ Q# `1 V
  70. //************3.删除图像信息************
    7 h: \% o* W4 I
  71. $url = "http://japi.juhe.cn/image_recognition/delete";
    & y* [) I( L( D5 J
  72. $params = array(
    2 Q4 r4 |1 ~/ d9 x0 s
  73.       "key" => $appkey,//APP Key/ R7 r- D1 h& T3 f8 l6 ~
  74.       "imageId" => "",//图片ID
    6 r: b$ p+ x( T% s4 ]7 R
  75. );
    8 Q% @  f" f9 l1 W' \" P) v" J6 T
  76. $paramstring = http_build_query($params);# X" w9 p% Q9 }4 V0 ]
  77. $content = juhecurl($url,$paramstring);% r7 c% n3 H' n7 }/ T
  78. $result = json_decode($content,true);
    0 a. H# L3 _/ r* ^/ m+ N. }$ E
  79. if($result){
    " b/ O. K3 a+ d. B% ^, W
  80.     if($result['error_code']=='0'){! z& N5 e" L5 r% C
  81.         print_r($result);
    7 u& k9 T& S. g) ^4 u# x
  82.     }else{# N9 d$ ]! T( v, p; t& a, u% c0 L
  83.         echo $result['error_code'].":".$result['reason'];5 F" u7 Z% y. H1 T8 i" ]5 ?/ [1 _
  84.     }
    4 N8 P" y! M" _) @8 v. u# q
  85. }else{
    1 w6 s  `" @0 V+ A+ g" ^4 t
  86.     echo "请求失败";; p2 z4 d$ B& m7 Z9 d
  87. }
    % Z4 H2 s+ |4 o, w9 {: X+ s
  88. //**************************************************
    ) c4 v- n7 ?; [1 P/ f! I$ u

  89. - t, v" @! F! o# X  u3 i0 g
  90. % f% z: a& B$ M9 @3 L) w1 N. u
  91. . m/ I: V( a1 I" |% C  K5 ~/ f

  92. : k7 T, r& Z3 K5 j, E
  93. //************4.图像识别************/ P# \& W8 \; E6 K. z% c3 U4 t* |2 P
  94. $url = "http://japi.juhe.cn/image_recognition/check";
    ) W! @2 j# v1 U
  95. $params = array(' H/ @% l  D3 g/ v
  96.       "file" => "",//上传的图片- C. |% a( y* b6 d, D$ Q
  97.       "key" => $appkey,//APP Key: P3 K" ?/ g7 z  O* K
  98.       "pname" => "",//包名# ^8 {2 S. B3 \1 b* @6 H1 Y
  99.       "device_name" => "",//设备名称(如android OS或iphone OS)7 p' \  S- z- Y
  100.       "device_version" => "",//设备版本(如:4.0.3)2 ~) b$ \/ d" ]! g
  101.       "latitude" => "",//纬度; w8 u8 b7 M. O0 s/ e9 E
  102.       "longitude" => "",//经度! o/ l6 S! Y2 a4 M- J8 u
  103.       "uuid" => "",//设备id
    9 i& S6 q2 b' r
  104.       "zone" => "",//请求地区+ g; e6 ^$ e5 g
  105. );
    0 }4 h( }( g- U9 z( ~6 g5 k+ v0 ?
  106. $paramstring = http_build_query($params);6 I; q" a# W% l3 {- A- J
  107. $content = juhecurl($url,$paramstring,1);5 N+ y, B0 e  F4 M
  108. $result = json_decode($content,true);
    : I8 W6 m( d; w8 F1 F
  109. if($result){, G8 `" D1 S: U: f& _$ M# E
  110.     if($result['error_code']=='0'){
    " U& l: b6 x. m# N; Q, [2 M
  111.         print_r($result);
    * }/ z8 `* R: ]( W
  112.     }else{
    ; |, J/ [- b; `
  113.         echo $result['error_code'].":".$result['reason'];6 [/ I( [2 Y. z/ @, t& _( M
  114.     }
    3 Y( s7 A% J, u/ s: q3 F
  115. }else{7 J/ l% _& n7 {: B- x, g* Y' s8 l' _
  116.     echo "请求失败";, P5 }4 d/ c/ ?; V: _/ t
  117. }8 ^5 F& |) ~3 X3 t2 }7 q8 q
  118. //**************************************************+ f/ |8 L6 l- r8 n
  119. ' ?4 F; a9 R9 s. ^) f' [

  120. * m/ Z" O  ?/ J% c; r  q

  121. 8 i$ K0 R. [; O! ?# v' c
  122. ( H. q' \6 g+ {- g% y

  123. 1 P1 R) H9 W: H: R
  124. /**/ L. V" q, ^$ Z1 J3 y0 D+ u: g; G. }
  125. * 请求接口返回内容( q: p" \* G- ~1 P3 L( b+ {3 z
  126. * @param  string $url [请求的URL地址]6 N2 a2 D& Z/ c0 p4 w
  127. * @param  string $params [请求的参数]$ p$ u% B2 x  Q5 N$ j: F
  128. * @param  int $ipost [是否采用POST形式]: ]. \3 g: V( ~& i4 U$ ]
  129. * @return  string, B5 x& ?8 q6 Q: h
  130. */
    6 |4 q( G4 ~7 n5 M/ t0 R
  131. function juhecurl($url,$params=false,$ispost=0){& w; w) {/ x3 Q+ s( u" J
  132.     $httpInfo = array();
    . i( g9 q* w5 u0 a
  133.     $ch = curl_init();8 N$ }, _- [: B' e0 t; ~' N
  134. 3 U9 ]. K7 e! \2 U
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );3 v3 t( b- [; ^; A0 m/ b! j
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );$ g* E  E' R, }8 \$ Q. @; l8 r! J; W- Y
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );1 \/ d+ _" M3 O+ Q9 P
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    ( k9 J5 y6 d1 Z2 {2 j# ]9 z
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );' W3 b, `" l8 q' h. v8 u" d0 }
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);9 k* \; ?  k1 a  _/ k) u
  141.     if( $ispost )
    " a/ F9 f( \0 ]4 d0 R
  142.     {% [$ O5 e- m6 J) m
  143.         curl_setopt( $ch , CURLOPT_POST , true );. K  I8 e- y: u9 ^+ P3 o- B
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );1 g: R8 L. Y# C: ^3 Q& F
  145.         curl_setopt( $ch , CURLOPT_URL , $url );
    5 ]' _- e! W  Z0 O% H8 @$ Q8 _0 \" ]
  146.     }* \4 t4 G7 x# x% S0 U
  147.     else
    + t8 H. o$ b# W0 y. x. Q+ @
  148.     {
    4 g$ ]/ J( c4 X; x& a
  149.         if($params){( d. q( }" H* A
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
    1 Q& _* M; D  ?) C3 S+ U
  151.         }else{
    1 V7 ?  r- a5 `
  152.             curl_setopt( $ch , CURLOPT_URL , $url);
    ( x% `2 _3 n/ T) g
  153.         }* G6 _% a! G3 o0 v
  154.     }
    ; y0 ~2 B- H$ m( i  ?+ B
  155.     $response = curl_exec( $ch );; A8 v) `% D8 e% p
  156.     if ($response === FALSE) {
    " ~% P) M/ M$ T1 Q2 P
  157.         //echo "cURL Error: " . curl_error($ch);( B) r7 p- k2 s0 v8 e7 E. \
  158.         return false;
    2 f9 A* H  E8 A5 q# H3 Q
  159.     }, ^7 |; M! q3 L9 y4 |6 e
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );9 M0 q, M/ j% q6 M
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    . q2 P' p, B+ P  N# E% h
  162.     curl_close( $ch );
    * A/ X5 [# l9 ]
  163.     return $response;
    # V$ c0 f: ]; Z$ V) O6 R
  164. }
复制代码
6 y/ H# x) c4 u/ `: r( N2 c& j' z& f

; g: j$ k' u6 L/ U; }
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-20 04:36 , Processed in 0.062997 second(s), 22 queries .

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