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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-8 00:13:40 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。6 [7 O  c/ w4 v( n
1.添加商品信息
$ K" w/ d/ \/ Z# C$ L# i- K/ n* ~2.添加图片9 t( n0 f) j" a# _
3.查询所有产品
6 e8 ?7 M1 L) f0 W; X) C7 I4.查询某个具体产品信息+ V7 w5 L2 w- a+ O5 b# \; \  a
5.删除某个产品6 G! Q7 `6 Y: ^$ Q. r5 p- s
6.删除图片
, t9 l( ?! [3 ?+ q0 ]7.图像识别
- G. h) x& r4 h8 R
% t0 I9 H! A4 I# t图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey1 k/ S; O& P3 l4 ^( {# I, d( M6 L
! `0 `, ~5 z, d; i$ D$ H" {: s
PHP示例:) ^9 i8 ]) d- F% X3 E* x
  1. <!--?php: z9 N) D. y+ f, |4 ^" h
  2. // +----------------------------------------------------------------------" P' g, f% e+ X) Q3 a' s* k2 o
  3. // | JuhePHP [ NO ZUO NO DIE ]+ U3 y, i$ i0 r$ ~- ]8 ~
  4. // +----------------------------------------------------------------------) N/ X: g% O! M4 W
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
    ( N* V* S2 k. c9 B3 i7 ~( Y
  6. // +----------------------------------------------------------------------- ?8 ~( g' L$ W1 |
  7. // | Author: Juhedata <info@juhe.cn-->
    # T# ]) ]3 E0 S# S) y0 I, p* G
  8. // +----------------------------------------------------------------------
    ' m5 u" a7 }( W, H
  9. 6 d- B, E  b2 k3 R
  10. //----------------------------------, _- Z1 M& z! Z+ ~
  11. // 图像识别调用示例代码 - 聚合数据
    6 k8 q+ B4 f' {7 t) i. ?
  12. // 在线接口文档:http://www.juhe.cn/docs/117: h/ x- o) m5 {. e( o# P7 d5 d/ P
  13. //----------------------------------
    7 L! X: H- d" }' J, j

  14. 4 J. }. a( R" E9 H; H
  15. header('Content-type:text/html;charset=utf-8');. f6 c5 m* j& l2 n! n0 e, u

  16. ! O% Q; s2 {. R7 S# _( [* g* i

  17. - W" F: S# L! I: r- _* `
  18. //配置您申请的appkey/ ?1 }8 g5 e" c) W
  19. $appkey = "*********************";
    & D9 q- m% l& n5 H' B
  20. 3 N- ~% x8 P, x

  21. ( W, z& k" D% a
  22. 2 r" U1 L, a, ]6 |( w1 H

  23. 8 g4 g$ K  _1 C; k  Y4 {' t" _
  24. //************1.图像上传************
    ' W1 I6 f& q  d6 L6 y
  25. $url = "http://japi.juhe.cn/image_recognition/upload";, D/ U1 W8 }/ V
  26. $params = array(
    4 N2 \  E  P& }4 j2 H) p! f/ @
  27.       "file" => "",//上传的单张图片
    & ~; a7 R6 A: O- u, U+ s* K( l
  28.       "key" => $appkey,//APP Key+ b6 P+ y7 h( N5 ^$ d0 C
  29.       "pname" => "",//包名
    # z* B* S% [9 q5 y9 f5 h
  30. );
    & ~/ o& U. O* f+ H7 T
  31. $paramstring = http_build_query($params);0 X: X, P; u( U5 d
  32. $content = juhecurl($url,$paramstring,1);! m$ s+ [6 `- C& T6 V- p
  33. $result = json_decode($content,true);
    / f. X3 b, l; }1 f% c8 \
  34. if($result){
    : \% T1 v! o; t! b3 n* Z. D1 ^8 |# j
  35.     if($result['error_code']=='0'){
    2 @! z9 }! _, P6 m+ I5 o/ O
  36.         print_r($result);
    + }5 C: ~1 f" ]/ [
  37.     }else{9 R6 j# X! W7 `6 c
  38.         echo $result['error_code'].":".$result['reason'];, w( S: u3 c, G- K& _3 d* T* K
  39.     }7 q% A, ?9 P/ I: q, H
  40. }else{. z4 e/ j3 K4 Z% J
  41.     echo "请求失败";
    ! L" w% x  N7 i5 _9 b4 w. J8 D
  42. }
    . x4 j3 q5 Y& M8 s# Y: ?6 q2 P
  43. //**************************************************
    % }$ Q4 Q2 c/ s% Y- u. H# R2 j" U7 y1 v

  44. 5 ^; f1 r% r' S7 b# [3 B( \' `( V4 s( t

  45. ! u9 y8 Z( Q5 o! G
  46. ( \: |% d% z. J$ @; e8 c9 X, n0 V3 ~
  47. 6 A$ |& {8 i) {( g- ]
  48. //************2.查寻上传图像信息************
    , A+ @1 p7 ?4 m, \. m0 m9 y
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";: U" m1 y. I' _8 X$ |/ w7 {
  50. $params = array(
    ; v+ N- w2 m, S5 C1 M0 l" p' \% x
  51.       "key" => $appkey,//APP Key4 f- ^9 {5 `. V& c
  52. );
      \( s  h7 g. a5 n" [% C0 b4 ~
  53. $paramstring = http_build_query($params);
    % z* Y8 C. H2 d9 z$ W
  54. $content = juhecurl($url,$paramstring);
    1 ?3 c) {3 L+ W1 f: X; W
  55. $result = json_decode($content,true);* b, P0 t+ N+ O, E1 S, m- ~
  56. if($result){. V+ R2 A0 L: [; }
  57.     if($result['error_code']=='0'){( V: a! b5 p% D
  58.         print_r($result);9 M! ^5 s( F/ s1 _0 V- k4 e4 y
  59.     }else{
    & l, a6 ]* ^$ G9 o, S& ]
  60.         echo $result['error_code'].":".$result['reason'];3 G1 f7 R6 ~, ^) f/ a7 N: I, |
  61.     }
    - Q: }& j4 T7 k) A
  62. }else{0 u: V: o; |7 j
  63.     echo "请求失败";
    . z  F: M0 P. I0 ]& ^9 C; E4 p
  64. }
    $ ~2 E2 P+ o1 q; k0 P  p! B
  65. //**************************************************7 l. o5 b: _) T: |0 b

  66. 1 Q* V3 J- A3 I7 \& e/ \' d
  67. ' G% j: V- |# O1 o* q) p5 J6 m

  68. ) C: d! H7 c: y: [
  69. 6 S2 v( X0 z& _+ Q
  70. //************3.删除图像信息************
    : r5 X% ~4 g7 X" ~! @3 A
  71. $url = "http://japi.juhe.cn/image_recognition/delete";- j( o$ v* }% R9 G( Y- Y( G9 q
  72. $params = array(
    " W; c3 I# c1 P6 M3 h# B
  73.       "key" => $appkey,//APP Key/ S8 u2 G1 i+ [' F5 f' f/ P* w
  74.       "imageId" => "",//图片ID# z) `4 G! ]. A) f# M  \: }+ }7 ~
  75. );, H* J' b4 N' U! o% w! F
  76. $paramstring = http_build_query($params);
    1 `+ J( c* w9 T1 b  [. C: _( s
  77. $content = juhecurl($url,$paramstring);$ Z" N1 q  i  v
  78. $result = json_decode($content,true);
    1 P! A# f% |! o. u: C% J( a
  79. if($result){9 G- `; ?2 I& |+ Z1 r
  80.     if($result['error_code']=='0'){4 B% }9 ?7 K5 i7 X+ l( C- V$ q6 N
  81.         print_r($result);
    & b& {. H$ b# X, p$ C, i: R
  82.     }else{
    + @8 B1 I' g- U5 I5 o' d2 Q
  83.         echo $result['error_code'].":".$result['reason'];5 d4 h7 N9 ?, S) b2 \
  84.     }
    ! \& j: }# r2 J- |" f( O. r, @
  85. }else{
    3 A% y, d4 Z0 w+ g% p- X  z
  86.     echo "请求失败";4 S, s7 _; i9 v% A( v
  87. }1 Y/ X' f% C* W% P! ?
  88. //**************************************************
    ) G- i% X1 p$ C6 V8 v( w" N

  89. 5 I$ p% x: g! g2 J/ c3 G+ x* H

  90. ! V- n7 \3 _! l% w+ l) m
  91.   z+ j$ ]* h* s( w# ^( J. d
  92. ; q3 A( T% [3 I  _3 i
  93. //************4.图像识别************3 r8 Y2 H3 K. |( I' p( ~
  94. $url = "http://japi.juhe.cn/image_recognition/check";
      W3 a7 k/ `; k/ r5 S  w+ H
  95. $params = array(' O) W6 N, g, J  Z  `3 M! Q) ~! M# W
  96.       "file" => "",//上传的图片
    % ]7 B  b! }1 i+ V& y, x
  97.       "key" => $appkey,//APP Key
    - K/ R; Y; p# @' p' a  x. A0 A
  98.       "pname" => "",//包名
    4 j0 L: P  M9 |$ O( c$ e+ `; g0 p
  99.       "device_name" => "",//设备名称(如android OS或iphone OS)
      i8 e. }" }& s& g) d* S2 l
  100.       "device_version" => "",//设备版本(如:4.0.3)
    ( s2 ^( ?# O" {5 K$ P
  101.       "latitude" => "",//纬度& |2 _+ H3 t$ }% M: d8 q
  102.       "longitude" => "",//经度" W" r3 C$ p+ {  M
  103.       "uuid" => "",//设备id$ T! F* l! [) E- Y* V
  104.       "zone" => "",//请求地区
    8 e! D8 o. O0 y& V
  105. );) j; D, h' i* k1 z- C6 b0 N8 I% r
  106. $paramstring = http_build_query($params);; Z. @4 `" L" N& g- Z# V
  107. $content = juhecurl($url,$paramstring,1);1 e1 r1 j/ `  r) d* o. P
  108. $result = json_decode($content,true);
    2 ?- _- c  T4 u( X- U
  109. if($result){) e9 |! c9 H4 L5 k8 F; b$ p& k
  110.     if($result['error_code']=='0'){* D- g/ p6 d- l9 _
  111.         print_r($result);" J+ T" V  b% _
  112.     }else{0 j7 a7 I# T% _. f$ k" Z% S3 g9 L
  113.         echo $result['error_code'].":".$result['reason'];6 l$ |% N  U& F/ N9 x% I
  114.     }7 S. t  F5 g7 c5 m/ A
  115. }else{( P8 x! o1 L/ k' e5 N; Z
  116.     echo "请求失败";
    4 r9 T/ N6 S# a6 T; D
  117. }
    9 n  e; \) @; a
  118. //**************************************************# u3 ^# ~7 a: K/ U' N! h$ h0 R4 T

  119. + y- v4 J4 b4 R! h

  120.   s- g' }2 ]) N2 ^
  121. % q5 J* t( }) w* g' |1 t5 k

  122. , w6 ^0 Z6 L) [. h
  123. + U2 L4 R7 K) M7 H+ ]! s
  124. /**
    / T- l! Y: c; ^  T# y% D
  125. * 请求接口返回内容8 }3 s% l, }: g- o7 o6 [
  126. * @param  string $url [请求的URL地址]
    * I/ ?1 j8 {1 y8 l# H
  127. * @param  string $params [请求的参数]
    9 j) |, ?8 O4 P& x* P
  128. * @param  int $ipost [是否采用POST形式]. M" l& H* M6 |5 _, s" H
  129. * @return  string* d* S! i! `: T
  130. */
    + _+ ?  w% p3 @
  131. function juhecurl($url,$params=false,$ispost=0){
    ! ~; C- {; d+ F. C
  132.     $httpInfo = array();' c' m4 J' w8 F. m; k
  133.     $ch = curl_init();3 ~0 t! L9 {+ \2 p3 ]! g: h
  134. - y9 Y- A0 i2 D
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );) h7 @$ r3 l- A7 ^; D7 P1 P
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    - \. T, x6 ~- ^9 h! Y' g0 }& o3 K. y
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    1 {8 v+ L" j6 r$ J
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    0 A$ j) i& N" \% H* ^: H
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );- Z4 g3 r5 W9 F$ c6 Y' x9 O
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    8 _8 Z2 c' }9 T3 M% X$ l* ?! m8 E
  141.     if( $ispost ), a" R) u; a) m9 c; h/ _
  142.     {
    9 t- }% t1 r) d
  143.         curl_setopt( $ch , CURLOPT_POST , true );
      F/ `% E: U0 D7 C
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );0 v: T4 f. T/ L* W( W( R, p0 U
  145.         curl_setopt( $ch , CURLOPT_URL , $url );
    ' i! ~8 }7 p. G( V7 v3 l, c
  146.     }
    . j' e7 Q9 Q( F+ h- u: B
  147.     else
    9 }) H( m1 d8 k* n* S
  148.     {
    ) I8 A; j2 c9 y/ l2 ?; `3 u: Y9 ^
  149.         if($params){
    ) S2 F" S* n! g+ _: h5 ]3 g
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
      w* e+ Q7 h! x
  151.         }else{: @( y8 O0 B. M% q
  152.             curl_setopt( $ch , CURLOPT_URL , $url);
    % J4 O& c. R8 \( ^& ]9 y0 a, l
  153.         }
    - u) H6 I; [. `4 m4 s- j" x8 T/ K
  154.     }
    1 ]% V$ q5 T7 F* P4 ?6 _
  155.     $response = curl_exec( $ch );/ `8 r; a( Y6 o8 C$ H8 `1 Q% j
  156.     if ($response === FALSE) {
    ( o4 D4 h" |& ^/ H% t) N7 Y
  157.         //echo "cURL Error: " . curl_error($ch);4 _3 t, R- f. i. P# f7 C
  158.         return false;
    9 t* n. a" B2 |$ o
  159.     }
    9 o; j! {/ Q5 x7 r1 g$ B
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    ' B6 p/ Z, H% @$ C0 E) Q8 t$ K/ y1 u6 w( l7 t
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    0 ^$ c6 u5 T! W/ g; n
  162.     curl_close( $ch );
    7 I+ {7 K0 }! v* Z. k# ]3 v
  163.     return $response;4 d2 d% _& [# M# B3 C0 k* l
  164. }
复制代码
1 @3 n( D. p. _+ t! B# F9 e

. U+ @9 R7 ?) x/ J
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 20:13 , Processed in 0.065601 second(s), 20 queries .

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