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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-8 00:13:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。7 z3 T( H: j2 E* y) d9 |
1.添加商品信息
3 R3 I# {6 x: N! U2.添加图片
% ~% G# c: E1 F; n1 O) T4 r3.查询所有产品, q9 D+ R9 V  C
4.查询某个具体产品信息$ e8 Q6 O5 S: [- \( [' w* e7 y. a  S( o
5.删除某个产品2 k/ M0 S% B" ?7 O/ ~
6.删除图片7 C$ t4 z  s( W
7.图像识别. P* x- i' t; Z( ?; K
4 O& ]1 k/ H5 K- h2 C; w
图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey! ?( y3 O8 E3 b+ a! {

" f: x/ V% q8 F% ~3 o. C, RPHP示例:6 a& V9 ~( e/ Q& l: b+ N- [) k3 u) `
  1. <!--?php& W$ B/ U3 I) k% X- j  d
  2. // +----------------------------------------------------------------------+ i8 F3 n6 |+ H# {! o
  3. // | JuhePHP [ NO ZUO NO DIE ]0 ?' _- R. x3 \. H; B( G  ~
  4. // +----------------------------------------------------------------------
    , x* Z7 `  j* ]$ Q: v# f
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.& V( y) B1 k8 O( p
  6. // +----------------------------------------------------------------------9 r9 l+ ]9 |! M, ~
  7. // | Author: Juhedata <info@juhe.cn-->
    - o- o5 k& K1 P* b" u
  8. // +----------------------------------------------------------------------
    $ U5 \9 }2 p" x* h/ L

  9. - w4 y& e( r2 B
  10. //----------------------------------
    8 M+ q5 x8 _$ r% l. V. d* G' Y( Z+ Y
  11. // 图像识别调用示例代码 - 聚合数据* i8 A$ A  e" d9 M" i. }
  12. // 在线接口文档:http://www.juhe.cn/docs/117( k& u. y- S" {& t" I) h- p
  13. //----------------------------------% g* A9 f4 T) Q4 h( ?; N' \
  14. 1 E. E& Q8 U# N8 Z
  15. header('Content-type:text/html;charset=utf-8');7 E+ G1 L$ U8 Y+ t( K9 G5 \  |3 ~: ~+ r
  16. ) v' T, R6 I/ B8 H- Z1 I( S5 p' P, \

  17. ( l* y6 T1 Y$ A0 B* Q- m* F' R
  18. //配置您申请的appkey3 F8 j& O) t7 Y1 r9 ~
  19. $appkey = "*********************";
    ( L  C% M/ R( z

  20. 5 r3 q3 h& C; y5 G6 g' S; y8 T' B

  21. & L0 s8 @- x+ D$ f) @4 u' P1 _0 e
  22. 2 V" D: a+ W* B9 q0 n* B

  23. " y. L. \+ a4 p2 I* a0 D' ]
  24. //************1.图像上传************( N4 S+ Q$ J3 B. Y# s0 j
  25. $url = "http://japi.juhe.cn/image_recognition/upload";5 L! ~+ M) q$ a# g
  26. $params = array(
    " e& T8 F$ X3 ]
  27.       "file" => "",//上传的单张图片" F# U! X4 B; W! A2 f* c$ W
  28.       "key" => $appkey,//APP Key: r2 `4 J- s8 x/ Q& o  L( }
  29.       "pname" => "",//包名3 Y0 a1 m/ Z" G6 m9 D" P" e* z: V0 \1 C
  30. );
    + U& l+ s: L4 `0 q5 F# `
  31. $paramstring = http_build_query($params);
    3 c# [4 {% G4 A5 s+ F9 C* O: @; s4 E
  32. $content = juhecurl($url,$paramstring,1);
    7 L1 L# n, |% J8 F7 F
  33. $result = json_decode($content,true);$ Y4 ?4 P/ O3 j9 n! i$ S3 U4 T
  34. if($result){
    $ U5 p2 E7 n1 _* ~/ Z
  35.     if($result['error_code']=='0'){
    , D' X/ R$ s# i6 D) n
  36.         print_r($result);* z+ Z. d8 |! P1 V
  37.     }else{3 K, \; O4 s) a. C% V
  38.         echo $result['error_code'].":".$result['reason'];
    6 ^( ^0 u9 H! a0 }
  39.     }
    $ o$ D3 ^& z! N# ^' c
  40. }else{6 R; T# `; V. b. Y- k2 A
  41.     echo "请求失败";9 b0 A+ Q$ B& F. ]: C. X9 X6 s4 [5 |
  42. }1 t, B: e* O& z& K
  43. //**************************************************
    8 I) Q5 x+ x- d9 N7 m. Z
  44. % v6 g8 V1 T3 r7 G# k

  45. , L- E; b3 B$ G$ A
  46. 5 Z3 m6 g$ K; j4 m9 q' R, C* @

  47. 9 c; x% G5 S6 P4 @6 k- f
  48. //************2.查寻上传图像信息************6 {& I: n8 Q8 r
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";+ d4 |( j8 @6 B  ~* H4 ?9 _$ N
  50. $params = array(: ^) R0 y/ [( D/ a9 d
  51.       "key" => $appkey,//APP Key
    + P% a7 U' ?1 d  v2 c. `$ ]
  52. );
    7 l% {* i+ g+ N/ l. r% v  d; E
  53. $paramstring = http_build_query($params);( M4 h+ ?( E% |- l: O) C, x
  54. $content = juhecurl($url,$paramstring);( A9 ^4 H, B- O! z3 [9 Y' T' ~9 s5 ?$ T
  55. $result = json_decode($content,true);3 s- V7 o' ]1 n% ~0 ?
  56. if($result){
    ' G" |5 l& d( k, _
  57.     if($result['error_code']=='0'){5 ?  Z3 D' {; s4 P
  58.         print_r($result);3 a9 i# }, A: n# }+ W- K& N6 G' M
  59.     }else{1 }' W% q6 |) W  q) H
  60.         echo $result['error_code'].":".$result['reason'];0 e0 d: N& Z! C  }
  61.     }
    . q. H7 e# O3 O- Y
  62. }else{
    $ x% M/ a% j' u% n
  63.     echo "请求失败";
    $ G# i6 o. f6 z$ `1 I+ r; ^
  64. }
    4 B- U. c/ u; k. E2 k# Y5 H
  65. //**************************************************# ?+ L8 Z+ S8 t0 r3 c* ^$ g

  66. ; r; d0 r; H: T, j0 E0 l% E4 C

  67. 8 t: d) f+ l' @1 r  V. \: g1 h
  68. 1 I% V5 Q! a0 R0 S8 c! k

  69. % ], W, E, P3 L" t, S! V
  70. //************3.删除图像信息************
      u; S( N" C! }* R" ~
  71. $url = "http://japi.juhe.cn/image_recognition/delete";
    1 @1 m' [) r( x
  72. $params = array(
    / C( @7 Y1 M0 M. @
  73.       "key" => $appkey,//APP Key* T; U* {$ v* \2 o
  74.       "imageId" => "",//图片ID! I/ Z8 B9 C* C" F
  75. );
    - N! D* f% {  J! S
  76. $paramstring = http_build_query($params);- H% H! M1 G1 E5 s
  77. $content = juhecurl($url,$paramstring);
    4 k" b* }2 m2 v+ z8 D2 y2 s
  78. $result = json_decode($content,true);
    : n2 L# W, ?  O: W' M
  79. if($result){; P1 s4 I- p* x) R2 Z1 h
  80.     if($result['error_code']=='0'){
    # N; Z: {/ t) x; K
  81.         print_r($result);$ z4 f( y8 Z6 W6 ~; V% `5 ~
  82.     }else{- ~6 f9 d2 S) ~
  83.         echo $result['error_code'].":".$result['reason'];
    " S* f4 v; u( Y7 l7 t4 W
  84.     }
    6 d8 x- k) k0 Y; d) C2 D+ s& S8 m. x
  85. }else{
    ! Z4 @; S, t7 n, k& b6 \
  86.     echo "请求失败";" f! B6 x9 I! }6 @; j1 C
  87. }5 u' s" f% S  @, k. k& p* p: g6 Y
  88. //**************************************************$ |. G  Q2 D9 {: A3 ?

  89. 5 |" D+ N# \3 W. H% _3 C6 m( o3 ?
  90. 4 v! R1 Z1 [8 O

  91.   X! O: b, [" I+ ]$ J
  92. 6 w, R0 F2 F; ^! }- j! E+ N
  93. //************4.图像识别************! R* t, Y; ^$ }4 x6 O
  94. $url = "http://japi.juhe.cn/image_recognition/check";  i  G" n' Q, B
  95. $params = array(
    ; G$ F; u5 @& w7 k: `* j5 ^
  96.       "file" => "",//上传的图片2 T) ^5 J' w0 p5 s
  97.       "key" => $appkey,//APP Key) I/ A- k  A3 g& s0 {
  98.       "pname" => "",//包名" @# l6 |! B8 D0 x; S. y8 z
  99.       "device_name" => "",//设备名称(如android OS或iphone OS). X1 ?: ~! \9 x7 ]' |5 A& e* z- |
  100.       "device_version" => "",//设备版本(如:4.0.3)
    8 X! b- y( b* h  P& W
  101.       "latitude" => "",//纬度/ H- x! Q: _4 q3 x
  102.       "longitude" => "",//经度
    : i* H, W! m; j! o6 P
  103.       "uuid" => "",//设备id- r9 n2 c2 N# S+ p4 t( C& k+ Q
  104.       "zone" => "",//请求地区& ~, w2 ~7 C/ _9 N
  105. );
      z& O& R- H9 J$ G
  106. $paramstring = http_build_query($params);
    ) v2 i1 O2 {. |. g8 ?1 m
  107. $content = juhecurl($url,$paramstring,1);4 G' ]: W2 S# f7 K9 b& M6 g! E
  108. $result = json_decode($content,true);6 y0 x+ o2 v. K2 y: U8 k! j
  109. if($result){
    ' D5 {# Z- }' k* D# u, e
  110.     if($result['error_code']=='0'){$ v5 D6 N) Z" o; Z
  111.         print_r($result);* j5 ]# J% |! U; j
  112.     }else{
    + X0 X* i8 |2 w8 Z6 A
  113.         echo $result['error_code'].":".$result['reason'];- W  L) }+ |6 c) g  n! l
  114.     }3 ~$ U4 l5 i. j( _$ N' G2 [
  115. }else{; m: S- W3 R/ i3 `( v( T
  116.     echo "请求失败";
    / C1 ~8 U2 m2 z) U2 \
  117. }
    0 a9 L  Y7 l+ {( [# O( P2 y7 k
  118. //**************************************************" d4 {& s& e3 u9 E/ J% B* M

  119. . a0 c$ ^# \5 o. {6 }% f- \

  120. $ q4 J6 p3 a9 n( M( f8 D# Q
  121. : Q! R) X' K2 W) |

  122. , j8 R1 t( [0 [" ~9 `7 K$ b

  123. 1 T. f* f# ]1 _% ]- t( U: O. x9 J2 T
  124. /**
    7 o! R; F1 T5 p
  125. * 请求接口返回内容
    # `6 U1 v; d4 X) G
  126. * @param  string $url [请求的URL地址]
    9 }8 B: |( ~( l' J
  127. * @param  string $params [请求的参数]0 N& L/ a% B* P, T
  128. * @param  int $ipost [是否采用POST形式]
    . z: ?- D- I. l0 @
  129. * @return  string% H: V$ Z4 d5 g  L8 o
  130. */6 B, Q' O( _6 c9 h9 H9 d
  131. function juhecurl($url,$params=false,$ispost=0){, Y5 d4 R7 p8 b3 \7 s& a
  132.     $httpInfo = array();, C1 |. d0 q1 n, F6 \
  133.     $ch = curl_init();
    ; W# e: B( o3 H$ Z
  134. 8 |8 c7 {/ V3 {4 g+ G! D; [- L
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );  y: S0 a9 Y6 M: ^; M
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    % Z8 D. ?. _, f  t' r
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );- f/ T) t+ o* U' `4 P  |
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    ; e4 E& [. ?- g) I; B; H
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );1 Z; ~( w# D  s) `) B2 y
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);. Y8 z7 G; l' q) k) q) B5 J5 I
  141.     if( $ispost )( N+ _  H% t% g# @, T- H
  142.     {" c6 ]1 t, D& o
  143.         curl_setopt( $ch , CURLOPT_POST , true );
    0 @% a7 Y: Y; i: T8 i/ t: N3 d$ k
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
    ; S/ P4 k6 s; q: W1 Y2 H
  145.         curl_setopt( $ch , CURLOPT_URL , $url );
    : U7 e9 ?4 ]5 x& m+ ~" g4 ]# y
  146.     }5 o5 X% k9 E, Y; a4 b4 K/ p
  147.     else. D5 W, c( `$ l: @( D
  148.     {) w- G  r+ S+ k6 T& i6 j/ I
  149.         if($params){
    $ a% u% T7 U% K% x
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );  O# W8 }/ g" Y
  151.         }else{- s% x# X! @# M5 {
  152.             curl_setopt( $ch , CURLOPT_URL , $url);
    9 {' v5 k0 r; Q* M& Q
  153.         }
      m" E2 f7 ]3 p
  154.     }! l" V3 v8 K! O2 x8 g
  155.     $response = curl_exec( $ch );
    + a3 V& m1 C  d( z4 ?3 C: u
  156.     if ($response === FALSE) {
    ! G3 R0 K. X6 P
  157.         //echo "cURL Error: " . curl_error($ch);& |5 }) x. I) H' T, S  W
  158.         return false;
    : z8 S) D* a  J
  159.     }
    ! k9 f8 W- Q( I) p5 K( r+ P/ Q
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );; v* J* s" k1 K$ v! n8 S/ Q
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );+ E) H5 p) F, `) J. M
  162.     curl_close( $ch );
    6 \+ ~! _. b$ M9 c3 s
  163.     return $response;
    $ p+ g4 F$ u  n( o
  164. }
复制代码

+ D  _2 [  B( @  I% N7 c5 J$ j, q5 `2 w: \/ c+ N+ G
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-19 22:26 , Processed in 0.119168 second(s), 19 queries .

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