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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-8 00:13:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。
- V- {0 d5 T- w4 {, [3 W1.添加商品信息
( q7 Z7 ?9 z7 B# |5 n* {; S$ z2.添加图片% Q2 S4 b& Y8 @* {! l8 X; V# {5 B
3.查询所有产品0 ?/ h2 u& G' b% M  Y0 M5 O! [$ s
4.查询某个具体产品信息* ]2 P) T* e1 ~1 m! a+ R- n: R1 R
5.删除某个产品3 }) M" e# `" F
6.删除图片
, ]; r2 O% w$ ~7 g- c) }  ?7.图像识别! S- U1 v) ?! ?- V) a' I

# c0 G. {0 |5 C, i图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey
8 P: X- R! \8 `9 v! c
' W# G; o2 |% W- k% L, {1 t# ?PHP示例:
2 ~0 i; D; [% [6 R+ C
  1. <!--?php- s. W! f# G$ L) b. e9 j5 t$ x
  2. // +----------------------------------------------------------------------
    + y' g; _& w! b" z& z
  3. // | JuhePHP [ NO ZUO NO DIE ]8 o  R! h. r1 ?6 M( k) X
  4. // +----------------------------------------------------------------------' P9 t- ?' |6 q; M; ~! x" f. |
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.' o+ E2 q3 ~, P+ o" c! H
  6. // +----------------------------------------------------------------------6 b' h; g. I( E7 H6 w; a. a1 W6 [+ J: D
  7. // | Author: Juhedata <info@juhe.cn-->
    ) w1 z- {- J  x: ^
  8. // +----------------------------------------------------------------------
    , i. i8 `1 ~( C/ M
  9. & o2 N$ \1 Y2 J# Y* _
  10. //----------------------------------
    " E1 K9 P3 Q3 M6 z7 i
  11. // 图像识别调用示例代码 - 聚合数据
    8 K  c; ~8 [9 c9 N: p/ L& s. c
  12. // 在线接口文档:http://www.juhe.cn/docs/117
    . P2 F& I5 ?. f- l: k5 s
  13. //----------------------------------1 I/ u$ r0 B( M# C: N  t8 c
  14. ( r; d- h8 O. d$ G
  15. header('Content-type:text/html;charset=utf-8');2 Z2 J+ Z# j1 ~/ A1 \8 o1 |

  16. 5 G; ?8 y, n" I  b( D

  17. , l. Z/ M  e$ d; i
  18. //配置您申请的appkey. y; S% E7 q8 e  c4 m' Y
  19. $appkey = "*********************";
    / s, X$ \$ ~/ r! J
  20. 1 `! Y  M/ |% I$ G) Z
  21. & W" O1 I7 @6 X, S% p

  22. % U8 Z% `& a, W& q

  23. 0 U3 x, a: ]. O' J" y4 N
  24. //************1.图像上传************3 `+ Q7 h) O% }3 _
  25. $url = "http://japi.juhe.cn/image_recognition/upload";
    / {  Y& }: c7 G' W6 k5 o; o
  26. $params = array(+ G1 L4 M4 C' X# Z5 W: a
  27.       "file" => "",//上传的单张图片' h) V0 C- _) I2 b6 ]5 q: A
  28.       "key" => $appkey,//APP Key
    5 Z% o! B3 p' G  i% K" T  c, d
  29.       "pname" => "",//包名" O' k6 S8 n2 M* j# \2 `8 i
  30. );
    ; y( b* n5 G2 N: V# ^% F: M
  31. $paramstring = http_build_query($params);/ Q& S- i) X4 K! Q. v3 s# M
  32. $content = juhecurl($url,$paramstring,1);
    * ?# n3 T' j: F3 ^2 V1 R9 C' I
  33. $result = json_decode($content,true);
    0 E' I* K/ g2 _9 z+ ]+ q
  34. if($result){
    . X1 p! T0 P9 S, R5 u  Z
  35.     if($result['error_code']=='0'){8 {0 P" i0 Y# d, O4 u  U* H
  36.         print_r($result);' q; s" G! }/ h/ q" |
  37.     }else{* _3 Y, m0 o( a2 N/ a9 X
  38.         echo $result['error_code'].":".$result['reason'];
    ' F6 Z8 {+ I; O2 }- B; U9 f6 M
  39.     }
    1 q% G' G) l  z# G
  40. }else{; D& a, ]; c4 }2 A' ?  ^
  41.     echo "请求失败";1 L* B& F- Q; M; ^
  42. }
    # ^% T5 s6 p% B' a
  43. //**************************************************
    ) A/ l" k: T  b8 q

  44. % }% u; V$ ]: y$ f% P
  45. , i" C3 z) T: X# q* v8 v
  46. / t7 w; q. M& p" i$ }: D

  47. ( b0 ], j5 `. c+ J8 U
  48. //************2.查寻上传图像信息************9 O) [/ a) ^  ~
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";
    5 ^0 r3 E% z9 v" p5 D
  50. $params = array(
    : ~$ ]1 f6 M. I0 j! {
  51.       "key" => $appkey,//APP Key5 K) s$ j* _# {. i% f# Z
  52. );
    . i1 F5 g& |7 h3 x$ O. m
  53. $paramstring = http_build_query($params);
    % v4 Z9 U; @8 l4 d
  54. $content = juhecurl($url,$paramstring);
    , E- Z0 W# q8 y, X, L) P5 w
  55. $result = json_decode($content,true);
    5 ?' s: D* G- l3 O( @9 y% t9 u
  56. if($result){
    7 _* e3 g2 P) U
  57.     if($result['error_code']=='0'){; V- l% {" {& J+ c
  58.         print_r($result);
    + r6 l" W) p6 Z
  59.     }else{
    / |! T+ z9 j7 g6 K9 Y
  60.         echo $result['error_code'].":".$result['reason'];; }. b" e% M5 l7 [; T: E5 p+ s
  61.     }# J) Q: J: ]: \5 t: R
  62. }else{0 b) t( p, K' J5 ?3 L& n
  63.     echo "请求失败";+ }! i  }' [1 ]* ~
  64. }9 ~) F: h7 T9 T3 ]1 r
  65. //**************************************************1 G8 ?+ ^6 j) w# y" U3 x  D2 K

  66. , w* C- a6 M& w3 M

  67. 0 _( y2 D- B/ K0 e0 W
  68. ( G  X' r" b' Z$ G  W8 A+ t

  69. # Y% X# J3 A3 r# _4 H0 W
  70. //************3.删除图像信息************4 U4 l2 _4 O: I
  71. $url = "http://japi.juhe.cn/image_recognition/delete";
    & h9 ~: o9 W# A% n' M
  72. $params = array(- S( U# {5 S/ r. ~, m
  73.       "key" => $appkey,//APP Key
    7 D% X' I: {( X& m: h3 a, A: w
  74.       "imageId" => "",//图片ID
    - g7 q2 V; |8 K) T
  75. );! Z5 g+ _, @7 y3 a
  76. $paramstring = http_build_query($params);$ Y0 N# T. y0 h$ ]5 L  n6 r
  77. $content = juhecurl($url,$paramstring);1 g8 y2 [" T! |- ^. v- h
  78. $result = json_decode($content,true);2 M( x( Y3 F) t3 B( L
  79. if($result){1 l" `. x& A& \/ K
  80.     if($result['error_code']=='0'){
    5 \2 q1 n9 j7 [. L' I5 I
  81.         print_r($result);
    - R+ R/ N. ]5 v8 E% Q& m
  82.     }else{9 D* \1 R9 o4 d7 s/ x1 ]% T5 Y
  83.         echo $result['error_code'].":".$result['reason'];
    1 d7 A7 j1 _! t2 i5 l
  84.     }( M7 ~% Y# t' _
  85. }else{
    6 V2 I$ z- [% a' ^- E; i
  86.     echo "请求失败";! C0 U: i+ Y3 D$ c/ k
  87. }
    $ P: v1 `1 @; |$ V9 a0 [2 \# ^) h
  88. //**************************************************
    3 K$ S- R- M) w! ~
  89. ) i+ `3 Q6 C  D
  90. 7 E' D+ u  A, R- G5 F2 G$ P# g

  91. / l- \# `: O2 r3 m/ x3 D

  92. 9 d7 D; {* D$ ]2 n6 o
  93. //************4.图像识别************
    ' v4 I, g4 {+ G" q5 a8 x0 I7 A7 w
  94. $url = "http://japi.juhe.cn/image_recognition/check";  X/ W6 U0 \0 H1 T, ^$ f
  95. $params = array() b# y! f: H$ |2 N+ t
  96.       "file" => "",//上传的图片
    % ^* |8 J8 G2 i/ {" _3 F
  97.       "key" => $appkey,//APP Key
    5 M( }& M7 J* |
  98.       "pname" => "",//包名
    ) W  c  d) M8 }6 a
  99.       "device_name" => "",//设备名称(如android OS或iphone OS)
    & i6 F) ~/ D6 o* c0 t' M0 \: ^# r/ f. ^5 w9 h
  100.       "device_version" => "",//设备版本(如:4.0.3)
    + a( f# E4 d  o  |
  101.       "latitude" => "",//纬度
    3 o" P4 y  S: i7 x
  102.       "longitude" => "",//经度) @' B% B- E  s1 Z& `0 H( r
  103.       "uuid" => "",//设备id( q% k* k4 a$ N
  104.       "zone" => "",//请求地区) Y# k7 i( n8 `8 Z1 I! P9 ~
  105. );) m( U; r9 ]% I0 n
  106. $paramstring = http_build_query($params);
    , f5 L8 p; D- k6 X
  107. $content = juhecurl($url,$paramstring,1);
    2 v8 f5 h1 y. J: b3 V
  108. $result = json_decode($content,true);
    4 r4 u4 H  i5 d& p4 f
  109. if($result){9 P* P0 |9 l- Q4 L4 v
  110.     if($result['error_code']=='0'){
    ; t3 [/ m5 B: _8 h3 h5 V
  111.         print_r($result);3 E9 E2 n. E8 L$ M# H; ]
  112.     }else{) ^! @( O! \, B# j
  113.         echo $result['error_code'].":".$result['reason'];4 B6 F6 W4 }1 T) y9 i% ?
  114.     }
    ' z- _+ q: a' [( v0 z/ I" H+ _
  115. }else{9 k7 }2 c/ \6 v, O2 O: U
  116.     echo "请求失败";
    & p8 a  A# ~6 z* o9 _9 q
  117. }* K: A  f2 ^7 T" e( k$ ~+ [# ]
  118. //**************************************************0 I6 I& T5 Y7 b, O8 y( E
  119. 6 n1 g: h% v4 A8 v  F' H, O

  120. , G8 A8 I( U2 f) X% F' ]
  121. ; n) S: `% q: J, S0 m
  122. ) r- V+ A8 A2 f) O; U5 Z& U5 x
  123. $ g& I* M- _  Q0 ?2 }5 r- p
  124. /**+ e! ?6 p& M" t( a7 D
  125. * 请求接口返回内容
    5 D" n& V; }9 b1 y$ x% K5 b
  126. * @param  string $url [请求的URL地址]* n' }" T! v+ [( f
  127. * @param  string $params [请求的参数]
    4 _. j8 L4 g( w; B$ Q' I1 w
  128. * @param  int $ipost [是否采用POST形式]
    ! O% y8 I6 r& o' o
  129. * @return  string, s: I3 O0 c) c
  130. */4 b+ i: I' U! F$ O) i, ~" L
  131. function juhecurl($url,$params=false,$ispost=0){
    . w1 X) w0 i) i& _0 f9 I
  132.     $httpInfo = array();
    - Z; N- M7 U: X# d4 y
  133.     $ch = curl_init();5 m, \# B0 A! a* y6 ?# T& E
  134. 0 t8 r& n* c' i0 q
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );0 D  X! I; ^. J! x
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );# j& |/ u' ?' e  v( F  n
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );0 b6 |4 K$ F8 H% y
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);' C: ~2 P3 d! ~+ w+ H4 \8 t
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );% u. B$ w5 ^* q! s9 }
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $ h  O9 L  H2 f/ P0 w! c
  141.     if( $ispost )# `1 U& G) \+ r1 m7 B, q- u
  142.     {$ ^& G; q: y9 `6 v8 J
  143.         curl_setopt( $ch , CURLOPT_POST , true );
    & U. E; H& S) i* T. a$ K; E, V
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );8 l" l+ T) i8 S5 \* A
  145.         curl_setopt( $ch , CURLOPT_URL , $url );/ r- E; ^3 V5 s' {- }! A: P
  146.     }
    , t# l9 }5 b7 B) r8 o6 h
  147.     else
    $ {4 U: n; x/ t( t4 E% v
  148.     {$ V$ L. T; s$ `) N6 w) z
  149.         if($params){
    ( z+ B. ^! L1 `! A
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
    ( `* v/ i6 C7 C: a4 E3 a
  151.         }else{3 X% I/ W7 f( w. ]
  152.             curl_setopt( $ch , CURLOPT_URL , $url);/ t$ n% Y0 v6 ~- t2 }
  153.         }
    ' S8 u7 c9 P3 {! @/ e4 u$ T  b
  154.     }7 q; D) V9 I9 G5 h) |* Q* x% G' F; c) e
  155.     $response = curl_exec( $ch );1 ~* S; g6 E8 c+ N' p4 M
  156.     if ($response === FALSE) {
    % c& F- V: `2 M9 h8 Q6 Y, M
  157.         //echo "cURL Error: " . curl_error($ch);
      \$ s' X# D- [. x5 Y$ B1 o
  158.         return false;
    2 D. j% O, F( H* y  ]! e7 v* O; d
  159.     }
    & U) f; \  |6 W2 L7 A3 ~( d! m
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    ' E7 d0 B% `- e5 ?8 L
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );8 x. A/ h  u. r/ D$ n, u
  162.     curl_close( $ch );& c5 r+ m( P  w0 Q; R) j: T' l
  163.     return $response;
      L8 I( z, v3 Y) ^, z1 z6 p* y
  164. }
复制代码

& I* u4 |+ D0 A$ X# R% t3 }5 P9 ?% M: I% `/ |
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-7-19 16:59 , Processed in 0.067653 second(s), 20 queries .

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