cncml手绘网
标题:
图像识别API调用代码实例(PHP)
[打印本页]
作者:
admin
时间:
2018-7-8 00:13
标题:
图像识别API调用代码实例(PHP)
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。
! r; z& C8 r$ J7 e+ N# A
1.添加商品信息
' U% G0 A3 k _. G/ l
2.添加图片
0 X9 R4 R) i! d$ P9 N
3.查询所有产品
; S" x W/ `0 n* ?+ N; J
4.查询某个具体产品信息
9 Z8 `- W7 x8 K, L! b9 D
5.删除某个产品
& D# W L D/ A6 Y
6.删除图片
4 |' X ~; q ~7 c( x8 h% z2 F
7.图像识别
5 z: N. j' t! O( D
7 M; f V) F* M/ T! g) Q
图像识别API文档:
https://www.juhe.cn/docs/api/id/117
,申请后生成AppKey
' C$ n8 \; u" r
- P+ z s: {( Q* s
PHP示例:
}* b9 ?7 T5 X6 [
<!--?php
5 f# w, i, y6 M& k9 Z
// +----------------------------------------------------------------------
/ n, \2 O7 f* g" k' ~5 s9 _
// | JuhePHP [ NO ZUO NO DIE ]
2 _3 n, b8 z% {7 Q6 D/ l; g
// +----------------------------------------------------------------------
* x& C# s7 c& o: n6 p& c' c
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
4 ^. U# a: M9 r- w0 I0 |7 p% z
// +----------------------------------------------------------------------
& B9 [5 @& t/ x# V
// | Author: Juhedata <info@juhe.cn-->
$ }8 j* }7 G( J
// +----------------------------------------------------------------------
5 Q, V7 h1 n$ {. `) N9 K9 c1 I
1 V% z. r* y( O* ]
//----------------------------------
* `! g) b7 i" N5 c) L, p
// 图像识别调用示例代码 - 聚合数据
. x7 W* p6 R: D' B6 z
// 在线接口文档:http://www.juhe.cn/docs/117
+ S3 B" L; B* J7 b' o5 e
//----------------------------------
5 V& j U' z- f" K0 W/ L7 W4 @
; `6 X! \" |) H2 x) ]! a" _1 G
header('Content-type:text/html;charset=utf-8');
; J, N1 S6 _6 l; s" m/ `# {0 U/ c2 v
l' E/ Q( m) l; C. X- q
4 h2 e, ~0 J$ ~4 l9 O, v# ]4 {& z
//配置您申请的appkey
" G2 k5 Z$ O. b* X
$appkey = "*********************";
- T0 Q! L; C$ L$ I7 z# e
. Q/ C8 Y4 g" B. G0 R; [
' a; i; S* C8 m0 M$ b/ S5 k
2 H6 Z4 F4 t( K( Z D \
1 F8 i2 Q9 h& R7 F
//************1.图像上传************
3 P: V( f2 l/ r) w- ~/ |2 x- F
$url = "http://japi.juhe.cn/image_recognition/upload";
% @1 Z. J& M1 a8 c) _1 @* U7 q
$params = array(
# |8 x2 X) ]+ L4 i* A
"file" => "",//上传的单张图片
' D; }& g$ j/ X
"key" => $appkey,//APP Key
7 E- @- {, j, u! w2 D' n
"pname" => "",//包名
/ U9 k! `! O2 r$ C/ S. D' q; F
);
. g) S; O$ Z4 [: D
$paramstring = http_build_query($params);
, t# h' T% D* o
$content = juhecurl($url,$paramstring,1);
2 Z' q' M/ f. C, d6 j. P
$result = json_decode($content,true);
6 L+ p" ]3 F! f9 S( T# B
if($result){
- m7 S4 I4 }) I( E1 S
if($result['error_code']=='0'){
; p& {1 p) _% g1 c1 z6 p4 z! t, }
print_r($result);
2 w7 p$ t! j0 a! H% \ D5 x
}else{
0 ]7 }. Q0 Y$ j* c
echo $result['error_code'].":".$result['reason'];
! l: a* U6 ^. o3 N0 U8 h" ]) Z* p4 M
}
2 _7 {+ Z+ D( y; S- Z# J
}else{
& |! r8 l4 G e5 m. [( ^$ Q+ N2 d
echo "请求失败";
6 K8 Q5 n: q4 ^8 M6 O
}
, X% m. b& c( Y$ ~( q4 y6 H
//**************************************************
% X+ P6 ^: Z9 y
5 v2 l: D2 Z& `# @6 a
/ n- t$ g) C: h8 u3 Z, G
' |" O$ C/ j" h r
- ~9 I7 [& t( u- T0 w$ T8 M
//************2.查寻上传图像信息************
3 i+ c; O' U; e: k/ Z$ u; r
$url = "http://japi.juhe.cn/image_recognition/findAll";
3 I! x7 }5 ^! |' v; j+ z5 S
$params = array(
' M7 B% q" S$ | E4 Q3 u
"key" => $appkey,//APP Key
2 h v, k m9 C0 D4 Z5 [( s
);
0 s! t% ~/ [* n
$paramstring = http_build_query($params);
) a. I" \- _3 Y
$content = juhecurl($url,$paramstring);
' H( N: q6 @4 ]9 I: J
$result = json_decode($content,true);
7 ~* G( M; n E4 E
if($result){
2 g5 S$ R# u' R( F7 J
if($result['error_code']=='0'){
5 e5 Z. ^) V& p( Q, I) k1 h
print_r($result);
1 g+ Q o) C. I5 t/ D2 x
}else{
/ ]1 C* q9 m( o8 A/ ]
echo $result['error_code'].":".$result['reason'];
6 q: J1 M6 n2 u" A9 n/ @
}
9 j9 I% j' Z5 @5 I: D0 K1 N
}else{
7 B" s. d2 \ ]
echo "请求失败";
) L1 z8 D& V% a& z% @
}
$ U' a# n5 o) C& Y9 }9 B
//**************************************************
5 V2 Y" u7 c* w$ f
; }- \% O0 r; P2 T# o( b+ K
) K: Z, G! l! \: u/ a n
1 o% Q; b! R9 }9 a2 u
t- L3 Y7 V/ h3 Z1 e
//************3.删除图像信息************
3 y7 \ D$ f, H6 B
$url = "http://japi.juhe.cn/image_recognition/delete";
/ ~7 L) k) z0 G9 [( `* V" T
$params = array(
: {/ [4 J. S8 P9 {% K
"key" => $appkey,//APP Key
+ {$ W: R2 g( c) @1 W- i- ?
"imageId" => "",//图片ID
" R9 H' r* R0 g* V0 Y
);
6 z x8 m0 H# I: ]1 I
$paramstring = http_build_query($params);
- l3 w; G4 I# ~/ @3 f6 m; i
$content = juhecurl($url,$paramstring);
/ I/ x1 n. d4 R, R# u
$result = json_decode($content,true);
- g9 f( o6 ~; w2 B* [
if($result){
, |* F2 B# Q- x' u, q! y8 M3 Z/ R2 i
if($result['error_code']=='0'){
) B2 \+ _ T! ?
print_r($result);
7 N, k! C! @$ _$ A% I+ G$ e9 c3 V
}else{
3 D% N n7 W% t7 o2 e
echo $result['error_code'].":".$result['reason'];
! i+ r8 ?1 g- D6 I1 j
}
$ n" f2 \$ J {' M4 T
}else{
/ A/ I5 _5 p6 |9 b6 `
echo "请求失败";
$ ]1 y0 }$ p! v d4 x2 t1 ]
}
, ]- `/ f" p' D5 C6 ~
//**************************************************
" U! s# A, B; H. j+ w; z6 @5 Z
- R" d/ x7 s/ t' N$ o. B
( x) T6 T C- s6 U
) K- X' o1 l8 }4 Q$ J/ r! {
. I" B; i& M( u! m R2 w
//************4.图像识别************
7 m! H) C. k- @( V2 C
$url = "http://japi.juhe.cn/image_recognition/check";
8 u I2 t6 ] g4 E1 S1 N
$params = array(
) `) c, D4 `- W1 t
"file" => "",//上传的图片
% [# |4 h& h6 c
"key" => $appkey,//APP Key
d- s( }/ F, V' r* S+ I
"pname" => "",//包名
2 P8 x. j5 T0 N) d& J0 m, {( z% n
"device_name" => "",//设备名称(如android OS或iphone OS)
1 Z, _4 Z) o* R8 i
"device_version" => "",//设备版本(如:4.0.3)
T$ f( ~3 V4 y1 q1 Q
"latitude" => "",//纬度
( I/ _5 \2 X0 ]7 U; A
"longitude" => "",//经度
0 o* @3 n. D3 \6 f% X$ p/ x
"uuid" => "",//设备id
" I, c6 [9 R1 z# f
"zone" => "",//请求地区
) X+ M5 Y/ c5 V
);
6 [( ]. T2 S0 |' s
$paramstring = http_build_query($params);
: \; h+ i. h+ a; Y
$content = juhecurl($url,$paramstring,1);
- z% |2 o( t: h4 J6 N% b
$result = json_decode($content,true);
) y/ O4 ?! B; i8 k7 S
if($result){
+ f& J b- t. p' |; h- C
if($result['error_code']=='0'){
) M! y, n3 s. P
print_r($result);
2 z% B5 Y {" k0 ^$ k
}else{
8 |( |' c, e; b# I, p, x2 ]
echo $result['error_code'].":".$result['reason'];
, Y9 ^: C7 P' @/ T5 v9 Q; h! I" I
}
" L" \0 f) }# {
}else{
7 f4 p9 |, |# j
echo "请求失败";
: z3 F$ r3 u8 l% ] Q/ d
}
$ g V3 c0 A) `0 i5 Q6 i: U! a" E
//**************************************************
& t# C8 V# E2 G+ I
+ d/ r5 W; z) ^4 S! B8 K! o0 q/ r* N
7 O) f* M0 e: \ C# z
! G. ?: B0 {0 s M, I# c) B
1 D2 E' s+ l* w$ h8 a& j
8 j; _6 y0 Z3 e* r# u% O/ {- M
/**
3 h% q5 ? J' v: T. i
* 请求接口返回内容
; k+ p5 p0 h" ^
* @param string $url [请求的URL地址]
9 o; D! M% u/ x. N% K. G
* @param string $params [请求的参数]
2 S) F- ^$ |0 Z0 r
* @param int $ipost [是否采用POST形式]
2 N ^. X# F/ k1 T* P! Q: @
* @return string
: ]. e" I4 X; ]1 ]: C6 i
*/
7 L9 S. |$ j5 f( M6 _1 Y$ H+ i
function juhecurl($url,$params=false,$ispost=0){
- K2 e0 J! Y e8 i, W% G
$httpInfo = array();
) d4 B8 I: l( t
$ch = curl_init();
1 i: E' p! x9 Z8 i; {/ b
' k. B# F& L' m/ ~+ }
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
9 [8 R0 B R4 r6 d, u
curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
! z* a( u- k; j
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
* I% m0 {. ?. @
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
& V( i, u: Q3 f6 K' k/ i
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
1 J4 Z1 S0 p# R
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
L4 Y! |7 O, y, ]
if( $ispost )
4 y# k1 r7 ]. l2 E, p
{
/ H; `/ P; f* |$ m3 B2 J
curl_setopt( $ch , CURLOPT_POST , true );
! k4 Y6 ?4 A9 W; A) U
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
" n4 R- { x, `' ]* T$ E
curl_setopt( $ch , CURLOPT_URL , $url );
0 e4 {" D* i8 J& b D
}
" w' |$ c* l! y1 l/ b
else
4 x+ r" x2 o1 u9 I6 p
{
# b5 d" s+ _. {$ b0 y
if($params){
) E9 V% r$ c3 D' U
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
: h5 V# x: B$ \3 n V
}else{
* E" m1 H' l/ u* k* t
curl_setopt( $ch , CURLOPT_URL , $url);
5 k! a' h% A/ i( d' l
}
' `! ] d8 ~. |: K+ R7 l/ d
}
) {3 p+ n9 C3 L; `$ d
$response = curl_exec( $ch );
& u- y7 z4 m$ c+ O0 Y) ~* w
if ($response === FALSE) {
5 M" Q7 h7 w* W* ]; G8 D' t" i
//echo "cURL Error: " . curl_error($ch);
! D) S' k5 N: P ]5 N% b
return false;
& J. D A$ f2 M1 ^! N0 Y
}
$ d6 `6 n z; l1 q/ F& C) w5 C$ G& j% J
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
& u7 G* B! S4 B8 I9 N
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
4 T" s- _, v% y% i5 o
curl_close( $ch );
7 e# G y1 w: d3 z u( N
return $response;
+ o5 d6 I6 L& F
}
复制代码
7 W! v o; J8 c- H5 t
# w0 A, ?4 _' j! V! ]9 r
欢迎光临 cncml手绘网 (http://bbs.cncml.com/)
Powered by Discuz! X3.2