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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-8 00:13:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。
/ ^2 i" ~" J1 I$ n1.添加商品信息
0 h$ [8 ]* |: ?. {2.添加图片# q( B" z; t3 V8 W  r7 i2 n
3.查询所有产品
5 Y; {+ W1 H, f: T' Z( R4.查询某个具体产品信息
3 E( X$ G& N! X' c! _  |9 ?/ @5.删除某个产品
" g+ Q- N. B& r! j: [: ^/ M6.删除图片) Z$ t' H% G! l1 P
7.图像识别
) |% b) x& H, c; D5 i6 `' o: k/ b) U8 r4 z: U4 ~( `) U0 I
图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey/ h# l+ V5 u& E( F3 k7 \
5 z5 A7 m* C# q9 w9 _" Z
PHP示例:+ o  w5 Y6 n8 J" B. z5 h. x
  1. <!--?php7 {0 o# G0 K0 C8 {9 r
  2. // +----------------------------------------------------------------------
    . u' a9 L0 s8 v# ^2 H
  3. // | JuhePHP [ NO ZUO NO DIE ]
    # w6 h0 x7 [, o  T+ Y
  4. // +----------------------------------------------------------------------  U, H5 a+ R0 q9 w9 ~2 S
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.3 F  N# R: N# Y% x6 u
  6. // +----------------------------------------------------------------------+ s% a: _/ L/ v% f2 c
  7. // | Author: Juhedata <info@juhe.cn-->
    5 l1 W$ t2 F8 u" `/ g3 f, c" a
  8. // +----------------------------------------------------------------------
    # k9 O9 w4 ?3 X% N4 o

  9. ( ]" ^0 u- J, D
  10. //----------------------------------
    ) ^" [5 T$ V* w
  11. // 图像识别调用示例代码 - 聚合数据
    ! p' b% f6 I5 b3 o9 X: }' r! b
  12. // 在线接口文档:http://www.juhe.cn/docs/117
    8 J6 N& A5 f) p
  13. //----------------------------------" A, U, M4 ]8 {+ p2 f- R% e. B
  14. ; @% T8 g- r# p2 T" J
  15. header('Content-type:text/html;charset=utf-8');. r9 c: x- E* v' g

  16. 8 v8 I+ {4 g$ d4 N$ v* p. f

  17. 3 I9 z: d# x+ y+ [
  18. //配置您申请的appkey
    ) y2 V" s4 }( U/ j) J% n
  19. $appkey = "*********************";5 e, m( I* u" P( s# ?# h

  20. 0 I0 ]3 Z" K( j

  21.   F6 t* ^" }6 q

  22. * ?% M- j6 z2 @; c
  23. , \  F( U6 }2 Y# |% J( J" d; f8 K: V1 H
  24. //************1.图像上传************5 ^) g  V& S, W) T6 }- i3 ~$ v1 ?
  25. $url = "http://japi.juhe.cn/image_recognition/upload";9 W' O* E  G, E5 \  [
  26. $params = array(3 f$ t5 u+ F2 H9 [9 l% m
  27.       "file" => "",//上传的单张图片! n0 ~* Z) v5 @
  28.       "key" => $appkey,//APP Key! G1 X0 z- m2 r* U+ U* L! ^" b9 w9 Q
  29.       "pname" => "",//包名
    . x4 o$ `! E* T5 Y
  30. );
    ' C- w# l. n% W. N1 \" W  p
  31. $paramstring = http_build_query($params);
    ! l! `) j9 v  s3 o- h
  32. $content = juhecurl($url,$paramstring,1);
    6 h# A7 |' w/ {& j% A: I$ ?
  33. $result = json_decode($content,true);% \! N. o4 {5 V. W+ ~
  34. if($result){
    $ x* T% I- C9 {: y
  35.     if($result['error_code']=='0'){! Y; v3 [+ P' I6 T
  36.         print_r($result);: u/ R3 ~. L7 o; [& u" h
  37.     }else{" Z/ i$ p. D6 \0 g: G
  38.         echo $result['error_code'].":".$result['reason'];5 X8 _4 H) F- F* n& v9 k
  39.     }2 Q1 C& M1 g$ y, v8 I
  40. }else{& x! E4 U' g1 k( ?# w4 ], z2 z
  41.     echo "请求失败";; L! E+ R  e5 z, W7 \0 `
  42. }9 l' a& k1 J( J& k" `
  43. //**************************************************- I  h/ X0 Q- t7 E, @0 l: W

  44. ) |+ V7 ?; c; [- m$ l

  45. 5 O# z4 r7 O( ]- }% o- j

  46. ! B. O4 c; Q+ h) |

  47. 4 n* t9 ^! Y0 E. a
  48. //************2.查寻上传图像信息************3 X6 m% i! N* W/ M; l8 [; I
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";
    5 z9 x$ p7 m2 r5 J2 O
  50. $params = array(. @; k9 z/ `# ]! d& z
  51.       "key" => $appkey,//APP Key- e$ q  M9 A& m
  52. );
    ; N! i  N: L2 X! g: Y5 _# N# S6 h- w
  53. $paramstring = http_build_query($params);' {, {" h. ]- N1 F1 ^% l/ c9 B
  54. $content = juhecurl($url,$paramstring);+ J1 W8 y7 H, z$ m4 x
  55. $result = json_decode($content,true);
    & O4 R5 T9 K  l$ Y
  56. if($result){8 y) h4 ]; U4 M6 o
  57.     if($result['error_code']=='0'){8 _4 @  n! V" x5 O2 x
  58.         print_r($result);3 \. C, {+ c2 \; d9 m" v1 m
  59.     }else{
    / w6 P; i3 n+ s2 X. t
  60.         echo $result['error_code'].":".$result['reason'];
    $ x  d: m6 A% g6 N
  61.     }
    . ^7 A" f8 R- ?9 [  y
  62. }else{; I' X' \4 `/ K+ ]
  63.     echo "请求失败";
    / `' l1 B2 q! }; S
  64. }2 ~' D. h4 V, n- n* `
  65. //**************************************************& s0 ^8 ^# l% l; _4 Z, s
  66. # A4 l* W7 N4 |+ l
  67. ( \7 `7 t" u) @/ M5 f- z# x4 R

  68. $ Z: F  |. E2 @, Q: j
  69. % R! K7 p$ b& B7 p3 D% [
  70. //************3.删除图像信息************' }; ?, A: Y# T
  71. $url = "http://japi.juhe.cn/image_recognition/delete";. I: @6 B; f0 s1 P' F" h
  72. $params = array(. i0 H1 [8 C, Q0 j7 Q
  73.       "key" => $appkey,//APP Key
    7 g2 A3 j& u4 {; r% M3 R) r
  74.       "imageId" => "",//图片ID
    9 _5 V4 _! Y( q; Z8 z% J
  75. );
    ( n. b' r7 v" N, @5 E) [, H) ]
  76. $paramstring = http_build_query($params);
    - Y% {( `# ?/ S; e
  77. $content = juhecurl($url,$paramstring);7 i) Y7 ~. M5 g& Q, t8 i
  78. $result = json_decode($content,true);4 X  _: A% {8 {: ?+ _/ i" ^
  79. if($result){
    3 p" u& h: l+ I1 r
  80.     if($result['error_code']=='0'){
    0 o) O; P; f% r4 _
  81.         print_r($result);4 D5 j, S! m8 h) W: k
  82.     }else{
    + x$ C1 H. [2 Y, r6 \  Y
  83.         echo $result['error_code'].":".$result['reason'];
    3 h+ G% C- v" e/ c
  84.     }
    : X% ~' P" t' K1 z
  85. }else{3 _' U4 i4 d5 o  W
  86.     echo "请求失败";9 D! A7 R5 C  E) h: z& F! ~% x+ M
  87. }7 ^- n1 Z! r$ C7 t. ^  X, n$ }2 U
  88. //**************************************************7 L- L( H- l0 c. O* h- |( ]' S: h

  89. ' O+ [8 K; I8 S, U3 t' o( F8 d  W/ }
  90. ; |9 h5 k, D: N( z) [3 Z8 c

  91. 0 I1 L4 c; ?2 W3 B

  92. 2 c3 C& r3 J, t+ J, E1 l- f
  93. //************4.图像识别************: v" o- s6 z2 C/ @/ g
  94. $url = "http://japi.juhe.cn/image_recognition/check";$ n/ V3 d5 t6 o
  95. $params = array(  C" k1 f% \2 D% j
  96.       "file" => "",//上传的图片
    % }0 c4 ~! ^# n/ w& E9 B+ [8 e
  97.       "key" => $appkey,//APP Key" E$ j, _3 Y, C4 d
  98.       "pname" => "",//包名
    ) w* m* X3 Q8 n9 l, _
  99.       "device_name" => "",//设备名称(如android OS或iphone OS)
    . O. A% f5 |/ I; m& H
  100.       "device_version" => "",//设备版本(如:4.0.3)
    8 i, V- k% k( b
  101.       "latitude" => "",//纬度; J" `5 `( r/ g6 l: Q: T  {
  102.       "longitude" => "",//经度
    / b- m0 `& Q" i) m9 o% S3 b" W7 Q; l
  103.       "uuid" => "",//设备id
    ' c- c  m' k- P
  104.       "zone" => "",//请求地区8 F+ a% H7 O* f7 Z+ X
  105. );; ?# a* q5 V# h3 A
  106. $paramstring = http_build_query($params);; B9 f! v: R& V2 l$ u- X
  107. $content = juhecurl($url,$paramstring,1);
    % n$ d: I# w7 k2 ?* m3 m
  108. $result = json_decode($content,true);  g: @: s) T: l+ _7 v
  109. if($result){
    2 n; V! n* ?. _% n
  110.     if($result['error_code']=='0'){
    ) G& z0 H! H  @" U! Q
  111.         print_r($result);
      j4 r- `2 H* B, u
  112.     }else{2 l  V# _3 x' H
  113.         echo $result['error_code'].":".$result['reason'];4 ~8 o' @3 |9 e  D$ ]- }) h% Q
  114.     }
    3 ]. v) T7 T1 W. A
  115. }else{
    8 G, ~8 R, g( ?0 u% [* F9 }
  116.     echo "请求失败";
    + @# O( X- s$ n% w# x& Y
  117. }9 \- l3 Q) R: Y5 E1 f+ t
  118. //**************************************************3 }" _& p( a: ?3 V. D* \$ _
  119. . x7 ?+ H1 z( i& [7 r
  120.   {& {6 d9 i. E+ [* j

  121. 2 H% ^/ t9 g" \% T0 b) K- I; V

  122. - k* T: G, C7 ?) V8 E

  123. % c) k$ I# v; S
  124. /**
    ; K" z! L( F& k6 Z
  125. * 请求接口返回内容2 a5 H+ y9 e9 n; n' u6 g3 q" N$ V
  126. * @param  string $url [请求的URL地址]
    : s5 k2 X/ j9 i5 L( t& M& K
  127. * @param  string $params [请求的参数]
      s, S8 z% |! |; }
  128. * @param  int $ipost [是否采用POST形式]
    ( H. u+ P4 N  |' C- B1 U) {
  129. * @return  string3 k+ K3 W# M0 n; |3 Z) _. L2 Z
  130. */
    ; T" r' D% D6 L; X
  131. function juhecurl($url,$params=false,$ispost=0){
      j! x4 Q5 q; X8 \% j' o% z
  132.     $httpInfo = array();
    $ |/ ?# ^% `2 G
  133.     $ch = curl_init();
    ' \+ N2 f$ E5 j

  134. : c9 n, {7 G9 R, d/ S2 ~
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    , }0 F7 w* ?+ F) D# h9 ^
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );- m: f/ m3 i, w0 r- z/ c
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );  n& @/ {! ~* O
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);( Q$ Y- U: d2 x( a8 w, j6 l
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );6 _* y; d3 r8 q: _
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    ' g+ {! V7 t! X
  141.     if( $ispost )
    4 v; L: R% u+ l, i2 w( g* R- M4 E
  142.     {2 b2 l# n' }/ z% V; Z, [* d
  143.         curl_setopt( $ch , CURLOPT_POST , true );
    . g8 l: i* X% W0 j6 j$ V) F1 T
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );* W% H9 u' y. U7 d. L3 a$ u; w
  145.         curl_setopt( $ch , CURLOPT_URL , $url );
    * v7 V0 _! o0 x
  146.     }' ^9 b2 e! j0 J; ^9 F
  147.     else; p; o* V% X. Q5 [+ t
  148.     {
      ?& f0 L2 t  d8 I5 @2 w/ B5 y# M
  149.         if($params){8 t& ]; M  C4 N7 S6 w. X
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
    ! f# W7 y) [' H: [
  151.         }else{2 g0 l) Q' W9 z& _( G( H
  152.             curl_setopt( $ch , CURLOPT_URL , $url);: Z4 Y# _4 F3 a) ?) p) {) w8 D
  153.         }) I; I. f; R3 T% A1 m4 W
  154.     }
    ' [( c3 r& c4 L& Q2 R
  155.     $response = curl_exec( $ch );
    5 L8 k1 [9 q" ]/ u$ z4 c
  156.     if ($response === FALSE) {
      u' d! g; F' w' e
  157.         //echo "cURL Error: " . curl_error($ch);; m0 `' i/ S2 U$ A: v- e. U+ q1 ~
  158.         return false;
    9 O  [; [: d, P; U
  159.     }! @. M7 }; h6 X3 p; u6 R% C
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    ! V" G  R# U1 _0 v' m# a. z
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    * A# \6 I1 R4 ]+ q/ K
  162.     curl_close( $ch );
    ! ~7 Y( Q1 \6 h- A
  163.     return $response;( e& S7 n  k. s& w% }. h+ O- z
  164. }
复制代码
0 M# L% ~3 b+ C/ u* u+ B* `* ]
/ l3 c1 o  W% W: `% L
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-17 10:18 , Processed in 0.114879 second(s), 19 queries .

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