cncml手绘网
标题:
图像识别API调用代码实例(PHP)
[打印本页]
作者:
admin
时间:
2018-7-8 00:13
标题:
图像识别API调用代码实例(PHP)
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。
}# h# ]1 C. F5 Y Q: b" h
1.添加商品信息
5 K* Z! @6 v! X+ r! q4 W( X" q F
2.添加图片
* h2 L. W" p( b# c* ^3 b
3.查询所有产品
5 s A! I: h# A0 h
4.查询某个具体产品信息
. C4 q2 F. S. k) c; Q
5.删除某个产品
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$ i
2 a1 W5 H" E; ]# ?0 P& T# v
PHP示例:
, Q9 F* D Q! }% ]
<!--?php
7 o! O! a4 E m7 B; ]3 m) |6 Q/ \
// +----------------------------------------------------------------------
* Q2 g' [2 h3 ~# j
// | JuhePHP [ NO ZUO NO DIE ]
# q9 p. Z3 k- f+ ^
// +----------------------------------------------------------------------
1 N0 E8 }% k" ` ?$ p, p
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
* a. G, v3 A# q5 m* E2 D( M
// +----------------------------------------------------------------------
$ M4 S& e7 \& T$ q! k
// | Author: Juhedata <info@juhe.cn-->
1 G+ n7 C6 o- I; R
// +----------------------------------------------------------------------
& r) B! O3 G+ c! X. J0 I$ \
9 i( O3 f% u3 r/ `5 ~5 N" L
//----------------------------------
; a/ x3 J% A% i* A* p7 [
// 图像识别调用示例代码 - 聚合数据
) C8 f9 R4 @( {9 r
// 在线接口文档:http://www.juhe.cn/docs/117
. q$ n! g' P; O
//----------------------------------
4 g+ w& g, ]5 j8 {
$ }" U9 q6 N9 f) x- S
header('Content-type:text/html;charset=utf-8');
, k$ f4 }7 z4 Z/ E+ F: t4 B" s
/ O$ ]6 \0 p) x/ {( d/ a, R# L& l
9 M7 `# G! u$ \! V
//配置您申请的appkey
$ f4 Z2 }" V' _0 S( h: T/ o
$appkey = "*********************";
. q; Y: d! N: U& b# k! M
# R. g0 Y6 a {+ g
3 s3 g9 ?/ Q& x( N) O
5 |4 P5 F( G9 X+ D; T
$ ]+ W7 b; x' D+ s+ U' ]! b
//************1.图像上传************
- J Y/ `1 z$ J- `1 U! M- q! _
$url = "http://japi.juhe.cn/image_recognition/upload";
" q; y& o' k! U I
$params = array(
2 K3 b* V5 H/ d9 C
"file" => "",//上传的单张图片
2 x# E! J% M3 V3 e
"key" => $appkey,//APP Key
3 z4 `" E. y& A* V9 X' F
"pname" => "",//包名
; r- X0 i6 g3 x/ B$ c& M( e
);
' d4 v% L& T% p& W& ]9 P" n0 o
$paramstring = http_build_query($params);
0 L# G: A5 `& E" k& p
$content = juhecurl($url,$paramstring,1);
5 I- z! `( i& t. `8 \
$result = json_decode($content,true);
+ z1 f6 c! w, y- r& d, |
if($result){
% J5 l6 d* o- R0 E' e9 y( ]3 t6 @
if($result['error_code']=='0'){
+ p0 K b2 I9 P+ M! P" l2 i
print_r($result);
( H7 \; a1 }% I, m: r4 E7 X) ]
}else{
/ ~$ q+ p( R+ }# N3 t& ^4 `
echo $result['error_code'].":".$result['reason'];
' D! a8 U+ U; {* [, t# {2 m$ ]' p
}
4 V0 R- ~6 d+ W1 k6 n* p
}else{
! _% Q9 b& E- U. s% \# I
echo "请求失败";
- ~9 c7 l ^, E$ J
}
1 t+ n; r! {8 F' z" `/ M- f2 h
//**************************************************
K8 M" u! t# ?! N( H
4 w( v& K* ^: n( k. L6 h& `& N
, k4 ?* B. H+ q$ C. k( W& S
2 }/ w% ?" l1 q4 p7 G2 U. t
2 a( g5 h( ]. N& X
//************2.查寻上传图像信息************
/ R3 { n9 r& m) E S# i
$url = "http://japi.juhe.cn/image_recognition/findAll";
8 D' ?6 F1 e" l
$params = array(
7 Y4 S Z( D' ]6 k/ L$ Q. U2 r) x1 a
"key" => $appkey,//APP Key
9 B5 [9 t8 w$ g2 k( o
);
2 k9 b: C. O$ @9 I# i) E
$paramstring = http_build_query($params);
+ j2 i- H% c9 _3 ]* y _. g7 ^; s
$content = juhecurl($url,$paramstring);
% l' @6 n$ m! h& L- x. ?
$result = json_decode($content,true);
% \( I8 Q( C0 x; e/ I2 y7 k
if($result){
6 I/ c9 M2 W, V4 v
if($result['error_code']=='0'){
3 ?( j) ^7 g3 d
print_r($result);
3 e1 c% `9 A3 _+ E$ R3 t
}else{
; x2 {' |8 S" c
echo $result['error_code'].":".$result['reason'];
% g- v& d* m$ a- e5 ?" d
}
9 i c. T- B% {3 L" ]: i
}else{
6 J& _7 e, l0 m, C- l* x8 ]6 @
echo "请求失败";
& J r5 P: i" t8 T
}
6 z% s" q' I4 m: Z8 r: \% e( U
//**************************************************
( n8 A- e' R8 w
) g s3 Q8 w8 m
, P/ v; M' d/ V. ]( G
2 x0 y& ^! `! Z
3 k R( M! A, F
//************3.删除图像信息************
8 Q- n' m! i8 N/ t
$url = "http://japi.juhe.cn/image_recognition/delete";
8 k* F# `; F% d* c5 j& V
$params = array(
6 |+ d7 }. k9 ]' P
"key" => $appkey,//APP Key
! J' o' r6 V( I
"imageId" => "",//图片ID
8 | q' m+ {! Q5 M
);
7 O- t7 }; K7 w% {
$paramstring = http_build_query($params);
( d7 w- i, t8 V- Q3 `8 g
$content = juhecurl($url,$paramstring);
( Y' ^' T; `* H# L( ]6 [
$result = json_decode($content,true);
/ N7 i; r8 E" r& \1 t
if($result){
5 ^& A- s' z- H6 ]
if($result['error_code']=='0'){
3 n1 W1 }6 _5 u1 V7 _
print_r($result);
, y% V a9 D; T# x% d; W4 b
}else{
' j I8 @, @( J9 X v
echo $result['error_code'].":".$result['reason'];
. i; |1 e' p) J' |
}
2 Z' Q* j" `% Z+ G# h1 u5 m* ~
}else{
* M p! z3 K; b9 r
echo "请求失败";
: W1 W1 Z7 k* X1 z
}
. Y. d& Q' Q6 E6 O. d6 m" V! e
//**************************************************
9 p- }8 v2 P7 W! y5 ] s3 K
' E; V3 j# e4 ^1 h
! a8 a0 G- e# i& A: `9 Y! E7 G1 w
8 o: M, k0 T* F4 x H% p
; e3 b- M7 W8 f2 z
//************4.图像识别************
8 K6 l: D2 a& `1 h
$url = "http://japi.juhe.cn/image_recognition/check";
( p- q. t3 e7 h, u9 c
$params = array(
# m( b( ?- c' S3 f+ |% L
"file" => "",//上传的图片
. a$ t! O b$ ~5 i" C
"key" => $appkey,//APP Key
8 W- ]; L$ N5 z* V1 _5 M
"pname" => "",//包名
* K0 c4 Q. I7 Y
"device_name" => "",//设备名称(如android OS或iphone OS)
+ Z" g9 f7 C+ H f' P
"device_version" => "",//设备版本(如:4.0.3)
! t! P' J4 }( I' I+ ~
"latitude" => "",//纬度
5 C0 ?' n* K2 x. x& f) ]
"longitude" => "",//经度
, [& d4 G k) Z X
"uuid" => "",//设备id
5 E9 b3 Q2 [2 P. G" z8 ^* W
"zone" => "",//请求地区
2 w7 R0 m7 b* k# i
);
3 ]8 P% I. c/ p" @ Y. o1 Z6 Y7 T1 e
$paramstring = http_build_query($params);
& o6 [: M' x: A# S$ p; u- {
$content = juhecurl($url,$paramstring,1);
! J+ ]7 }& ]$ I; E; Y7 L5 B2 d* X
$result = json_decode($content,true);
- C, l) y" S- B1 S# \0 Y
if($result){
6 o5 V6 a+ n5 ~! i% z. m5 U
if($result['error_code']=='0'){
/ @: S7 v# O9 i, w
print_r($result);
I& ^& F0 H- n4 [
}else{
& q. J. H7 d% j0 X
echo $result['error_code'].":".$result['reason'];
% y4 w, v& g4 b0 R7 E+ ?9 k( h
}
+ D5 N9 W$ g- l- p; w& n5 \7 U; f
}else{
+ Q" W7 |# L. V% y: B# m# P
echo "请求失败";
. J2 ^. k. ?! T
}
0 `- d1 z6 ?1 I1 h n& G+ {- `
//**************************************************
) t! k g2 d: G2 y
6 q' Y1 B' m+ }) P" Y% H$ Z) |$ u
7 d$ n) D2 W& E2 X- G" a
e$ U, T4 d$ @7 S4 w+ i8 O
6 Z* q8 P; U) j! u
1 I5 l" J. U& ^$ x' G, F
/**
# B; F+ Z! i% F7 T' X$ e6 D! W" i3 g
* 请求接口返回内容
1 T4 y* {$ d# P+ C4 S( z- o# C
* @param string $url [请求的URL地址]
; [+ }7 M7 w/ e% k: @
* @param string $params [请求的参数]
! n5 a0 V ]. e E9 U0 X
* @param int $ipost [是否采用POST形式]
' ?' A0 y4 q6 s# ^
* @return string
2 ?8 m' O$ i9 V8 Q6 J# `8 m* r- d
*/
* ^& n3 R' E# m. u
function juhecurl($url,$params=false,$ispost=0){
$ A @" f2 l1 w C
$httpInfo = array();
! o/ M, c) D; e5 \
$ch = curl_init();
8 v4 X6 [+ `, K; d, x
8 `2 C: ] s6 [ R G* t' J
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
6 S w+ b5 K7 s- i8 @% c
curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
- S. {* W; {( |( O0 Z
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
$ c0 m5 @9 k7 v) b. R9 N
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
& Z3 t L9 Y8 c
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
) K- Q1 [9 q- Y7 R+ A+ s
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
" q5 s# \$ O$ k* }5 g6 i2 \
if( $ispost )
$ A) R" d6 C7 C! Y8 Y
{
- o9 |5 f( W& k- D2 p. ~& J- q
curl_setopt( $ch , CURLOPT_POST , true );
J9 h+ F8 |: `
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
2 U& m5 M1 K) O5 p8 I; z4 z
curl_setopt( $ch , CURLOPT_URL , $url );
) A" f! u& g: E( `4 B! X# t3 c
}
2 E( Y3 _6 ]* K: q
else
, R0 t6 ~/ L5 a8 u0 z6 t
{
4 ^% K [6 U3 H# ?$ O3 B
if($params){
& ?" }) C, ` E
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
) ~" h# j5 Y' I$ y/ L% x
}else{
9 J: a9 O- p# ~3 n" l
curl_setopt( $ch , CURLOPT_URL , $url);
% Q6 h4 i p- [( g/ v+ x
}
* @: o$ W% o3 H( K4 m8 g
}
4 I( d, m, z3 z+ W& A7 O# `
$response = curl_exec( $ch );
& X, i* ^7 [7 y3 D; z
if ($response === FALSE) {
7 X1 a. M) X4 Z" c' q- F! u! w
//echo "cURL Error: " . curl_error($ch);
. D5 z' e/ k W( g U
return false;
" ?& Z/ V \5 X( Q) o. n, P" y
}
% j+ N, } @1 ? Z$ N
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
x6 f, a; l; G9 P, @3 h0 m
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
1 A! H& ^' v1 x9 ^
curl_close( $ch );
; K5 G; ?+ X! Q4 S% b! D4 Q. [
return $response;
* z# e" G: U# r
}
复制代码
, 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