cncml手绘网

标题: 图像识别API调用代码实例(PHP) [打印本页]

作者: admin    时间: 2018-7-8 00:13
标题: 图像识别API调用代码实例(PHP)
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。
  }# h# ]1 C. F5 Y  Q: b" h1.添加商品信息
5 K* Z! @6 v! X+ r! q4 W( X" q  F2.添加图片* h2 L. W" p( b# c* ^3 b
3.查询所有产品5 s  A! I: h# A0 h
4.查询某个具体产品信息
. C4 q2 F. S. k) c; Q5.删除某个产品
7 z  m% d7 X* i* }- o* |6.删除图片! b4 X. J: h- a- H  j9 m+ C
7.图像识别. R6 J* }3 V3 j5 X4 v9 H

8 G( ^$ B' {. D$ u( Y图像识别API文档:https://www.juhe.cn/docs/api/id/117,申请后生成AppKey
  i6 [2 e" B* |% }$ f4 j$ i2 a1 W5 H" E; ]# ?0 P& T# v
PHP示例:
, Q9 F* D  Q! }% ]
  1. <!--?php7 o! O! a4 E  m7 B; ]3 m) |6 Q/ \
  2. // +----------------------------------------------------------------------
    * Q2 g' [2 h3 ~# j
  3. // | JuhePHP [ NO ZUO NO DIE ]
    # q9 p. Z3 k- f+ ^
  4. // +----------------------------------------------------------------------1 N0 E8 }% k" `  ?$ p, p
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.* a. G, v3 A# q5 m* E2 D( M
  6. // +----------------------------------------------------------------------$ M4 S& e7 \& T$ q! k
  7. // | Author: Juhedata <info@juhe.cn-->1 G+ n7 C6 o- I; R
  8. // +----------------------------------------------------------------------& r) B! O3 G+ c! X. J0 I$ \

  9. 9 i( O3 f% u3 r/ `5 ~5 N" L
  10. //----------------------------------; a/ x3 J% A% i* A* p7 [
  11. // 图像识别调用示例代码 - 聚合数据
    ) C8 f9 R4 @( {9 r
  12. // 在线接口文档:http://www.juhe.cn/docs/117. q$ n! g' P; O
  13. //----------------------------------
    4 g+ w& g, ]5 j8 {
  14. $ }" U9 q6 N9 f) x- S
  15. header('Content-type:text/html;charset=utf-8');, k$ f4 }7 z4 Z/ E+ F: t4 B" s
  16. / O$ ]6 \0 p) x/ {( d/ a, R# L& l
  17. 9 M7 `# G! u$ \! V
  18. //配置您申请的appkey$ f4 Z2 }" V' _0 S( h: T/ o
  19. $appkey = "*********************";
    . q; Y: d! N: U& b# k! M
  20. # R. g0 Y6 a  {+ g
  21. 3 s3 g9 ?/ Q& x( N) O
  22. 5 |4 P5 F( G9 X+ D; T

  23. $ ]+ W7 b; x' D+ s+ U' ]! b
  24. //************1.图像上传************
    - J  Y/ `1 z$ J- `1 U! M- q! _
  25. $url = "http://japi.juhe.cn/image_recognition/upload";" q; y& o' k! U  I
  26. $params = array(
    2 K3 b* V5 H/ d9 C
  27.       "file" => "",//上传的单张图片
    2 x# E! J% M3 V3 e
  28.       "key" => $appkey,//APP Key3 z4 `" E. y& A* V9 X' F
  29.       "pname" => "",//包名; r- X0 i6 g3 x/ B$ c& M( e
  30. );
    ' d4 v% L& T% p& W& ]9 P" n0 o
  31. $paramstring = http_build_query($params);
    0 L# G: A5 `& E" k& p
  32. $content = juhecurl($url,$paramstring,1);5 I- z! `( i& t. `8 \
  33. $result = json_decode($content,true);
    + z1 f6 c! w, y- r& d, |
  34. if($result){
    % J5 l6 d* o- R0 E' e9 y( ]3 t6 @
  35.     if($result['error_code']=='0'){
    + p0 K  b2 I9 P+ M! P" l2 i
  36.         print_r($result);( H7 \; a1 }% I, m: r4 E7 X) ]
  37.     }else{
    / ~$ q+ p( R+ }# N3 t& ^4 `
  38.         echo $result['error_code'].":".$result['reason'];
    ' D! a8 U+ U; {* [, t# {2 m$ ]' p
  39.     }
    4 V0 R- ~6 d+ W1 k6 n* p
  40. }else{! _% Q9 b& E- U. s% \# I
  41.     echo "请求失败";
    - ~9 c7 l  ^, E$ J
  42. }1 t+ n; r! {8 F' z" `/ M- f2 h
  43. //**************************************************
      K8 M" u! t# ?! N( H
  44. 4 w( v& K* ^: n( k. L6 h& `& N
  45. , k4 ?* B. H+ q$ C. k( W& S

  46. 2 }/ w% ?" l1 q4 p7 G2 U. t
  47. 2 a( g5 h( ]. N& X
  48. //************2.查寻上传图像信息************/ R3 {  n9 r& m) E  S# i
  49. $url = "http://japi.juhe.cn/image_recognition/findAll";
    8 D' ?6 F1 e" l
  50. $params = array(
    7 Y4 S  Z( D' ]6 k/ L$ Q. U2 r) x1 a
  51.       "key" => $appkey,//APP Key
    9 B5 [9 t8 w$ g2 k( o
  52. );
    2 k9 b: C. O$ @9 I# i) E
  53. $paramstring = http_build_query($params);+ j2 i- H% c9 _3 ]* y  _. g7 ^; s
  54. $content = juhecurl($url,$paramstring);
    % l' @6 n$ m! h& L- x. ?
  55. $result = json_decode($content,true);% \( I8 Q( C0 x; e/ I2 y7 k
  56. if($result){
    6 I/ c9 M2 W, V4 v
  57.     if($result['error_code']=='0'){
    3 ?( j) ^7 g3 d
  58.         print_r($result);
    3 e1 c% `9 A3 _+ E$ R3 t
  59.     }else{
    ; x2 {' |8 S" c
  60.         echo $result['error_code'].":".$result['reason'];% g- v& d* m$ a- e5 ?" d
  61.     }9 i  c. T- B% {3 L" ]: i
  62. }else{
    6 J& _7 e, l0 m, C- l* x8 ]6 @
  63.     echo "请求失败";& J  r5 P: i" t8 T
  64. }6 z% s" q' I4 m: Z8 r: \% e( U
  65. //**************************************************
    ( n8 A- e' R8 w

  66. ) g  s3 Q8 w8 m
  67. , P/ v; M' d/ V. ]( G
  68. 2 x0 y& ^! `! Z

  69. 3 k  R( M! A, F
  70. //************3.删除图像信息************8 Q- n' m! i8 N/ t
  71. $url = "http://japi.juhe.cn/image_recognition/delete";8 k* F# `; F% d* c5 j& V
  72. $params = array(
    6 |+ d7 }. k9 ]' P
  73.       "key" => $appkey,//APP Key
    ! J' o' r6 V( I
  74.       "imageId" => "",//图片ID8 |  q' m+ {! Q5 M
  75. );7 O- t7 }; K7 w% {
  76. $paramstring = http_build_query($params);
    ( d7 w- i, t8 V- Q3 `8 g
  77. $content = juhecurl($url,$paramstring);( Y' ^' T; `* H# L( ]6 [
  78. $result = json_decode($content,true);
    / N7 i; r8 E" r& \1 t
  79. if($result){
    5 ^& A- s' z- H6 ]
  80.     if($result['error_code']=='0'){
    3 n1 W1 }6 _5 u1 V7 _
  81.         print_r($result);, y% V  a9 D; T# x% d; W4 b
  82.     }else{' j  I8 @, @( J9 X  v
  83.         echo $result['error_code'].":".$result['reason'];. i; |1 e' p) J' |
  84.     }2 Z' Q* j" `% Z+ G# h1 u5 m* ~
  85. }else{
    * M  p! z3 K; b9 r
  86.     echo "请求失败";
    : W1 W1 Z7 k* X1 z
  87. }
    . Y. d& Q' Q6 E6 O. d6 m" V! e
  88. //**************************************************
    9 p- }8 v2 P7 W! y5 ]  s3 K
  89. ' E; V3 j# e4 ^1 h
  90. ! a8 a0 G- e# i& A: `9 Y! E7 G1 w
  91. 8 o: M, k0 T* F4 x  H% p
  92. ; e3 b- M7 W8 f2 z
  93. //************4.图像识别************8 K6 l: D2 a& `1 h
  94. $url = "http://japi.juhe.cn/image_recognition/check";
    ( p- q. t3 e7 h, u9 c
  95. $params = array(
    # m( b( ?- c' S3 f+ |% L
  96.       "file" => "",//上传的图片. a$ t! O  b$ ~5 i" C
  97.       "key" => $appkey,//APP Key8 W- ]; L$ N5 z* V1 _5 M
  98.       "pname" => "",//包名* K0 c4 Q. I7 Y
  99.       "device_name" => "",//设备名称(如android OS或iphone OS)
    + Z" g9 f7 C+ H  f' P
  100.       "device_version" => "",//设备版本(如:4.0.3)
    ! t! P' J4 }( I' I+ ~
  101.       "latitude" => "",//纬度
    5 C0 ?' n* K2 x. x& f) ]
  102.       "longitude" => "",//经度
    , [& d4 G  k) Z  X
  103.       "uuid" => "",//设备id5 E9 b3 Q2 [2 P. G" z8 ^* W
  104.       "zone" => "",//请求地区2 w7 R0 m7 b* k# i
  105. );
    3 ]8 P% I. c/ p" @  Y. o1 Z6 Y7 T1 e
  106. $paramstring = http_build_query($params);
    & o6 [: M' x: A# S$ p; u- {
  107. $content = juhecurl($url,$paramstring,1);! J+ ]7 }& ]$ I; E; Y7 L5 B2 d* X
  108. $result = json_decode($content,true);- C, l) y" S- B1 S# \0 Y
  109. if($result){6 o5 V6 a+ n5 ~! i% z. m5 U
  110.     if($result['error_code']=='0'){/ @: S7 v# O9 i, w
  111.         print_r($result);  I& ^& F0 H- n4 [
  112.     }else{
    & q. J. H7 d% j0 X
  113.         echo $result['error_code'].":".$result['reason'];
    % y4 w, v& g4 b0 R7 E+ ?9 k( h
  114.     }+ D5 N9 W$ g- l- p; w& n5 \7 U; f
  115. }else{+ Q" W7 |# L. V% y: B# m# P
  116.     echo "请求失败";
    . J2 ^. k. ?! T
  117. }0 `- d1 z6 ?1 I1 h  n& G+ {- `
  118. //**************************************************) t! k  g2 d: G2 y
  119. 6 q' Y1 B' m+ }) P" Y% H$ Z) |$ u
  120. 7 d$ n) D2 W& E2 X- G" a

  121.   e$ U, T4 d$ @7 S4 w+ i8 O

  122. 6 Z* q8 P; U) j! u

  123. 1 I5 l" J. U& ^$ x' G, F
  124. /**# B; F+ Z! i% F7 T' X$ e6 D! W" i3 g
  125. * 请求接口返回内容1 T4 y* {$ d# P+ C4 S( z- o# C
  126. * @param  string $url [请求的URL地址]
    ; [+ }7 M7 w/ e% k: @
  127. * @param  string $params [请求的参数]! n5 a0 V  ]. e  E9 U0 X
  128. * @param  int $ipost [是否采用POST形式]' ?' A0 y4 q6 s# ^
  129. * @return  string2 ?8 m' O$ i9 V8 Q6 J# `8 m* r- d
  130. */* ^& n3 R' E# m. u
  131. function juhecurl($url,$params=false,$ispost=0){
    $ A  @" f2 l1 w  C
  132.     $httpInfo = array();! o/ M, c) D; e5 \
  133.     $ch = curl_init();
    8 v4 X6 [+ `, K; d, x

  134. 8 `2 C: ]  s6 [  R  G* t' J
  135.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );6 S  w+ b5 K7 s- i8 @% c
  136.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );- S. {* W; {( |( O0 Z
  137.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );$ c0 m5 @9 k7 v) b. R9 N
  138.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);& Z3 t  L9 Y8 c
  139.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    ) K- Q1 [9 q- Y7 R+ A+ s
  140.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    " q5 s# \$ O$ k* }5 g6 i2 \
  141.     if( $ispost )
    $ A) R" d6 C7 C! Y8 Y
  142.     {- o9 |5 f( W& k- D2 p. ~& J- q
  143.         curl_setopt( $ch , CURLOPT_POST , true );
      J9 h+ F8 |: `
  144.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
    2 U& m5 M1 K) O5 p8 I; z4 z
  145.         curl_setopt( $ch , CURLOPT_URL , $url );
    ) A" f! u& g: E( `4 B! X# t3 c
  146.     }2 E( Y3 _6 ]* K: q
  147.     else, R0 t6 ~/ L5 a8 u0 z6 t
  148.     {4 ^% K  [6 U3 H# ?$ O3 B
  149.         if($params){& ?" }) C, `  E
  150.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
    ) ~" h# j5 Y' I$ y/ L% x
  151.         }else{
    9 J: a9 O- p# ~3 n" l
  152.             curl_setopt( $ch , CURLOPT_URL , $url);% Q6 h4 i  p- [( g/ v+ x
  153.         }
    * @: o$ W% o3 H( K4 m8 g
  154.     }
    4 I( d, m, z3 z+ W& A7 O# `
  155.     $response = curl_exec( $ch );
    & X, i* ^7 [7 y3 D; z
  156.     if ($response === FALSE) {
    7 X1 a. M) X4 Z" c' q- F! u! w
  157.         //echo "cURL Error: " . curl_error($ch);
    . D5 z' e/ k  W( g  U
  158.         return false;
    " ?& Z/ V  \5 X( Q) o. n, P" y
  159.     }
    % j+ N, }  @1 ?  Z$ N
  160.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );  x6 f, a; l; G9 P, @3 h0 m
  161.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );1 A! H& ^' v1 x9 ^
  162.     curl_close( $ch );; K5 G; ?+ X! Q4 S% b! D4 Q. [
  163.     return $response;
    * z# e" G: U# r
  164. }
复制代码

, F' p, A& C5 r* v" O3 X2 ?) W
: d1 d/ ^4 w% |# n+ e1 }




欢迎光临 cncml手绘网 (http://bbs.cncml.com/) Powered by Discuz! X3.2