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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-8 00:13:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。
6 T) w1 J( q; n5 a; [, M1.添加商品信息  {6 q) d/ T+ {# ]  k9 c3 G
2.添加图片
) M* d' O' o' I8 C( U% i3.查询所有产品
" }) L6 g* d7 Q' F& R4.查询某个具体产品信息
' A- `9 j# L: A$ F, p6 B, a5.删除某个产品
9 O3 Q8 r0 k9 O" D" q+ B6.删除图片
: D5 ^% H) n5 X: l7.图像识别3 ~5 l. J) Z8 z) X7 a4 m. J) W/ o

" A! Z2 O! \+ r  K' v* s' x图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey5 X0 I# i4 Y! A1 u1 D" C7 r3 j

0 M/ d" \- U5 t. y$ C8 P1 ePHP示例:
; a) g/ @2 r4 t/ G2 j. ^+ ]
  1. <!--?php
    6 _" [! ^; ~8 I: h& d8 X
  2. // +----------------------------------------------------------------------  Z5 Z$ E' n3 _" S6 B2 a
  3. // | JuhePHP [ NO ZUO NO DIE ]
    $ h% j- b1 V, A$ g/ S" F+ P
  4. // +----------------------------------------------------------------------
    ( l8 C0 c* P+ ^1 e
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.$ @8 o. K! [5 i) n
  6. // +----------------------------------------------------------------------
    . R8 T/ c' P  A
  7. // | Author: Juhedata <info@juhe.cn-->
    0 m/ a8 z2 F: Z9 ]4 l6 s
  8. // +----------------------------------------------------------------------
    ; {; n% l& }+ Z9 `* O8 Z4 u1 Y' F

  9. + @1 c: L# U* G' }! l8 d$ b
  10. //----------------------------------! }* H4 f1 {5 I  T
  11. // 图像识别调用示例代码 - 聚合数据
    , z6 L0 S, \" e, h7 i6 M+ f
  12. // 在线接口文档:http://www.juhe.cn/docs/117
    : i4 Z. S# t9 Y7 d7 A4 a2 v
  13. //----------------------------------7 l. u( C- g+ d4 U+ ~, k9 m
  14. 3 L- _" L# g( g5 L( r5 l& y
  15. header('Content-type:text/html;charset=utf-8');( F& d7 k- l  v( a9 B0 U8 S: A% d8 ?
  16. $ s# W, \% N: C: l" j$ m1 m% I4 q

  17. # D, ^  e" I6 v& o. o
  18. //配置您申请的appkey; O9 r* ]: q  w# R3 Q! U2 C
  19. $appkey = "*********************";
    9 H4 Y+ w# Y9 N' a) o1 @6 E2 O
  20. $ ?' R' ^! ?2 Y! _. t  D* C3 P

  21. + ?: ]' s! `" i4 }
  22. 2 T: N& N7 e; R( r# G/ m  i# U- A

  23. ! y2 L- ?; P8 L$ t6 B3 ]
  24. //************1.图像上传************; a* _* V4 u; Z1 Q+ I3 ?0 `0 K" U
  25. $url = "http://japi.juhe.cn/image_recognition/upload";
    1 l. @# a: i4 W* r
  26. $params = array(
    1 U+ n7 k; `) F+ v' o5 w8 ]
  27.       "file" => "",//上传的单张图片, Y% f: i, s( \$ F# Y; w4 [9 ~
  28.       "key" => $appkey,//APP Key8 H; `8 \3 R3 x  O
  29.       "pname" => "",//包名
    0 X- `# D' O+ i0 ]. w8 _
  30. );
    9 g( z. Q. m1 a5 c" X3 q! d
  31. $paramstring = http_build_query($params);( |8 x. k, [2 p/ L4 u
  32. $content = juhecurl($url,$paramstring,1);
    3 l! [6 W; a- ]) l+ k
  33. $result = json_decode($content,true);
    , M$ @6 |3 @4 A( Z- _4 x! j6 U  d. v
  34. if($result){
    & I+ ?6 S7 D. v& v; o
  35.     if($result['error_code']=='0'){  @6 v* \- H  |% N4 w
  36.         print_r($result);- f% D: r0 S+ _" Q, `
  37.     }else{+ O' ?3 ^& a1 u% W+ y9 Q
  38.         echo $result['error_code'].":".$result['reason'];
    ; J0 i1 T, o1 J9 c
  39.     }
    ; x4 k" y0 Z% @. W7 b( P
  40. }else{1 F$ a: h  e" @% M& f
  41.     echo "请求失败";
    " i- K% y& B+ Z+ h
  42. }
    4 M* Y% j) n0 O8 z9 e3 V" X8 r
  43. //**************************************************- D5 d. |  C' d, h  w
  44. " K  `% D2 E( o5 P

  45. . [! c9 _& ?+ D4 Q
  46. ( e* B: T! Y1 X& }4 w$ h" |

  47. 5 ~3 L, [8 s8 _2 v: }& a; R
  48. //************2.查寻上传图像信息************: N, I9 F$ Q9 Y8 V( D) q9 I
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";
      M+ a8 f" c. d. i$ [9 u$ o
  50. $params = array(
    0 X( \  D( y+ B) D, _
  51.       "key" => $appkey,//APP Key. q; E8 }4 ]0 u( K4 w3 w: j
  52. );+ P2 U: D5 g: _. f7 x  {
  53. $paramstring = http_build_query($params);$ g$ _0 w) I' g5 @6 V- A
  54. $content = juhecurl($url,$paramstring);
    * c& z9 G$ q8 A! ^
  55. $result = json_decode($content,true);) e% o: M# F& ~" ^0 ?$ @
  56. if($result){
    ! l4 c2 v7 g/ ~; X4 e- J# H  k+ ]5 F+ l
  57.     if($result['error_code']=='0'){
    ) T/ b$ }% N4 x
  58.         print_r($result);- z" n5 y! |" J
  59.     }else{
    2 B+ e6 b4 W+ b. Q
  60.         echo $result['error_code'].":".$result['reason'];
    , e! b# N% Q7 r1 d
  61.     }
    ( `: |- {0 c& i" m. q4 k9 g
  62. }else{4 A# C$ P6 ?  A2 g# T: N
  63.     echo "请求失败";
    9 l/ [. h: C# B5 C" V" O( k$ T
  64. }
    6 Z/ ^: W; h2 C) m% e6 X% L" C
  65. //**************************************************
    & N/ ^  G' f, |8 R
  66. ) S" i2 E4 G+ O7 e
  67. , `+ F' z- c! r5 C: W4 c
  68. 5 n3 N) j% k6 W7 W
  69. + w  ]/ }3 H0 H4 a" }" _/ J
  70. //************3.删除图像信息************
    ! P: q: G) a# a- z' w
  71. $url = "http://japi.juhe.cn/image_recognition/delete";! i% d2 ^( x  }
  72. $params = array(
    0 g% u, H* f* W, H% y8 g
  73.       "key" => $appkey,//APP Key
    3 l! F! B1 u/ X1 W4 E0 A6 y5 y
  74.       "imageId" => "",//图片ID
      G5 I. y, r& E+ b8 d
  75. );
    ( C4 B) \' o, V2 j/ w. C
  76. $paramstring = http_build_query($params);: H, l' }3 U  i3 W5 a4 y
  77. $content = juhecurl($url,$paramstring);
    , y& z0 x9 C3 ^* ^
  78. $result = json_decode($content,true);, f2 I7 r0 M0 H
  79. if($result){9 Q" U2 m1 \1 r
  80.     if($result['error_code']=='0'){# H2 s, _& d, \6 P$ p" |8 l
  81.         print_r($result);$ I! ^5 p% _) f+ Z
  82.     }else{0 C( S- [  E& d' L
  83.         echo $result['error_code'].":".$result['reason'];/ o4 }  O8 S1 H% {
  84.     }. ?% ~* l# b: A/ T) ?5 x1 Q- J
  85. }else{$ H! I; _: g$ j& S/ c# M5 A
  86.     echo "请求失败";4 V4 R) ?. M& q, r
  87. }
    9 I0 G3 M1 y, R1 w" v( K
  88. //**************************************************& z/ f+ N! |7 [4 F) b: v8 y

  89. 9 T" N# ~/ |+ a1 x% }0 j
  90. ! p* E6 h" |% F% a
  91. 0 J. q6 w! a6 N/ L( ~2 _0 F& r
  92. ) G% E% {. g! P- v
  93. //************4.图像识别************& U, n2 @3 Q  b$ F
  94. $url = "http://japi.juhe.cn/image_recognition/check";
    4 ?9 i! W1 z, p7 L5 i4 d9 N4 L
  95. $params = array(
    9 V% d& a, m) g3 b( @3 ?/ O
  96.       "file" => "",//上传的图片+ y- l0 [/ _; o0 e: L6 B
  97.       "key" => $appkey,//APP Key/ Y9 x" V5 U( d: }( r! ^* r# F2 f8 G
  98.       "pname" => "",//包名
    9 ]4 j, T! n8 [, J1 e3 R
  99.       "device_name" => "",//设备名称(如android OS或iphone OS)
    3 L* \6 l$ E* j  \
  100.       "device_version" => "",//设备版本(如:4.0.3)
    ! I  j, U. X: I( _- Q
  101.       "latitude" => "",//纬度
    ) P2 q0 W) `! u! F$ N- E
  102.       "longitude" => "",//经度* d) e% O: Y: x: @
  103.       "uuid" => "",//设备id
    1 v( n7 E7 R- q9 u5 o
  104.       "zone" => "",//请求地区
    6 Y% m( Y% f$ N; ^3 |7 u1 U
  105. );
    4 e6 F" `' m3 Y
  106. $paramstring = http_build_query($params);* e& G& t5 p5 k- t7 C' [, Q) |
  107. $content = juhecurl($url,$paramstring,1);; A3 K3 W1 q7 e7 c; P* L- ^
  108. $result = json_decode($content,true);* b$ T  I& m2 M' w( Z* W8 |' j
  109. if($result){
    % t6 X' p- u7 _) O
  110.     if($result['error_code']=='0'){( t+ \) T7 k$ @
  111.         print_r($result);
    & ^5 D" P0 k' a( F1 y2 L
  112.     }else{$ e; T; e" Z. }$ f7 [4 r/ Y
  113.         echo $result['error_code'].":".$result['reason'];
    7 _$ i3 d* ?$ B4 `& X% J8 S
  114.     }
    , T3 Q, \9 ]$ F% O# N
  115. }else{& f2 f; l# q. [
  116.     echo "请求失败";
    * m4 a+ g% U  p6 a% M
  117. }8 a6 R9 D% f) I% s/ A5 g
  118. //**************************************************3 W3 D7 B0 B% _- z+ z  o
  119. / J+ Y+ k# x* y. v6 E9 w: ^1 ?
  120. # F) I' G0 b( |: K* t

  121. 8 i  _# ?: |/ q2 b% H8 b
  122. " K+ ?1 d4 N3 a
  123. ' ]% ^9 N. H* G" N) H0 e
  124. /**& Z  c5 Q' l  `6 H9 n' u8 g
  125. * 请求接口返回内容% V: ]5 Q4 U" E7 u, l  ?! u
  126. * @param  string $url [请求的URL地址]7 [" |/ q/ w- e8 G1 v
  127. * @param  string $params [请求的参数]
    2 q9 |$ L5 Y2 D
  128. * @param  int $ipost [是否采用POST形式]
    % R  y% u9 g& q9 u9 ?6 @4 N
  129. * @return  string/ h" d# W0 u  P6 R: g  }: ?
  130. */
    & y5 p' q! T6 D' @+ m/ r4 Q
  131. function juhecurl($url,$params=false,$ispost=0){# t' n. U: P; j/ {, [! [6 ?3 V1 \
  132.     $httpInfo = array();
      m& C, O! w* {, c0 Q1 n/ w# _+ h7 _
  133.     $ch = curl_init();) g/ |8 S1 X" [- D# ~3 T' g
  134. 6 E1 S, S  c' i3 ~0 H" `  {
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    ; e- x/ |* k2 [: d
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );  M! G# h/ ?, P9 _, c& g
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    * c2 _) W9 J4 D1 f  q; C" r
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);* q3 T3 M1 s8 ?: w: o/ u
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );5 y, s8 f+ h! T, Y- \
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    ) H$ l- n- `1 ?- z
  141.     if( $ispost )
      T$ h  o+ n4 S" r7 F
  142.     {2 O7 {1 ~& b$ G8 X2 ~. v' Y" K
  143.         curl_setopt( $ch , CURLOPT_POST , true );! B2 I5 I- J3 K# g! o
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );/ a  [; Y, l7 W7 ~1 O& z5 t9 l* W$ m
  145.         curl_setopt( $ch , CURLOPT_URL , $url );
    8 j1 X  I0 a, g' S' Q
  146.     }) ], h- {- P1 \3 @$ B7 c" [
  147.     else* R# P0 Z$ y9 @0 {, p- L5 ^+ o
  148.     {1 J3 Z$ c: Q% z( T8 F' q2 \$ b, R
  149.         if($params){1 v: Z; l0 P# y0 G( ^
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
    , e3 Z# {8 Y9 B0 }8 c( A0 D$ P
  151.         }else{" T; N2 ~# ], ?# ]$ i
  152.             curl_setopt( $ch , CURLOPT_URL , $url);2 `$ Z  ~4 F7 z
  153.         }) `) |5 E5 o- T) J# Z, |  U9 M
  154.     }
    1 u4 Z1 m( c& m5 R0 Z# w) q
  155.     $response = curl_exec( $ch );. m0 [, r  h9 b* x/ p6 p7 T. g2 u
  156.     if ($response === FALSE) {# n" w6 q0 r. T- U- ^8 ^
  157.         //echo "cURL Error: " . curl_error($ch);
    ! A' y+ ~1 g1 j( i
  158.         return false;/ n4 T0 N/ Y, r* x4 M9 v& p: A( y6 \+ C
  159.     }% w" T# A# p& P; Z/ L7 \5 y
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );4 K5 p1 X% V- z6 n
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );6 i- |+ N# \4 K& U
  162.     curl_close( $ch );
    . o3 g! M. f" b; Z& {. r
  163.     return $response;* ~  H6 a% Y; j# k; W
  164. }
复制代码

# F; u" v- r' f( Z  X2 T
% P& U" j( m6 W: i
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-17 21:48 , Processed in 0.057962 second(s), 20 queries .

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