cncml手绘网
标题:
图像识别API调用代码实例(PHP)
[打印本页]
作者:
admin
时间:
2018-7-8 00:13
标题:
图像识别API调用代码实例(PHP)
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。
8 U+ a P: r$ T+ e! E
1.添加商品信息
\4 S1 \" u! ^ A) y+ |
2.添加图片
- Y, ]7 O% ~6 }5 y' Q( A l7 X; q8 O
3.查询所有产品
6 r E& l. g ^3 w. @2 g# b' `4 ^
4.查询某个具体产品信息
: G# p2 y' X" ~1 i. z+ e: v/ S6 n
5.删除某个产品
9 g4 l9 }9 A, r! L! B2 b
6.删除图片
. ^) b2 z9 H3 z! G' p3 P8 ~
7.图像识别
& \. ^6 V7 b- r
_' v9 ~2 M! U
图像识别API文档:
https://www.juhe.cn/docs/api/id/117
,申请后生成AppKey
& A( s4 i a+ [# O4 ~( _$ ?
( Z2 [% H0 w% B& J' J) @
PHP示例:
, W n* ]6 Y, ?0 x2 x$ {( U8 m o8 O
<!--?php
0 l* }# s) |! G% d2 d2 [- k
// +----------------------------------------------------------------------
$ Y5 d. x0 L" Z( I
// | JuhePHP [ NO ZUO NO DIE ]
# x5 L# w1 p- [8 @( O0 B' L
// +----------------------------------------------------------------------
1 _3 x) D* N; d
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
# ]- T/ L) A( i2 [- \4 M! r3 t! l
// +----------------------------------------------------------------------
3 I. M: c7 C( g- A$ I6 O
// | Author: Juhedata <info@juhe.cn-->
- J6 U# u& {2 }
// +----------------------------------------------------------------------
9 K% E2 w$ b1 p% \$ _ t7 p/ }
2 M) o! p6 ~5 l) x2 ^% M+ a% Z
//----------------------------------
& N/ d) I4 x8 U N6 A
// 图像识别调用示例代码 - 聚合数据
0 l' K [& b8 e+ Z/ Q6 L0 d
// 在线接口文档:http://www.juhe.cn/docs/117
* }# v' p4 q8 p$ \
//----------------------------------
/ q' K6 C3 n( P
4 n3 a$ O. V2 r: ~' \
header('Content-type:text/html;charset=utf-8');
4 ]" ~) v1 m5 [) y; e Q
. x T+ v" @7 u/ Q
0 ^! }. @8 t$ H; l* i$ e, L& R! _
//配置您申请的appkey
. K" Y8 T, D k1 [+ R7 [, w
$appkey = "*********************";
2 M$ Y+ C: M; S7 ^' _" X) P; c
2 Y. F+ x( F; A w
' n2 Q6 Y& ~' V! z. r4 c
& _) F2 j* ~) B D. G' C
' W: q6 L+ X$ J
//************1.图像上传************
' n* }+ u8 F$ I
$url = "http://japi.juhe.cn/image_recognition/upload";
- r8 T; G$ L# u8 Y% o$ R
$params = array(
5 ?! K# ? e2 G! D( e# g9 o
"file" => "",//上传的单张图片
. i2 M& m" l l7 _+ l4 b6 i& n
"key" => $appkey,//APP Key
4 ~4 ^- R" x; N4 `/ z5 [
"pname" => "",//包名
* {# j8 e- }6 s
);
" _! q0 T. h2 S t8 h/ F2 W3 ~
$paramstring = http_build_query($params);
$ b0 w& k! t* E) l$ a8 g
$content = juhecurl($url,$paramstring,1);
5 d' F; b7 e7 D& T& k" S3 l
$result = json_decode($content,true);
7 A5 x% T1 Y- J, k, m' S
if($result){
4 @" U% l3 S, R( w3 H2 ~0 b
if($result['error_code']=='0'){
7 o- Q3 N7 P; k }
print_r($result);
H9 @ ^" H7 Q& ?- O& f* m
}else{
: \1 t: Z5 B4 L9 P3 b
echo $result['error_code'].":".$result['reason'];
3 }2 C4 t% d% Z! q
}
J, K6 x: ]8 s6 k, D
}else{
+ n; E" U% b8 U4 `- E
echo "请求失败";
( ?! C( y. \/ F4 }1 N! c; I( A
}
) A. |; V+ B# c3 a
//**************************************************
9 W& a1 B3 U) v: L
+ t* L, N( c& w( h r0 G- l
' f8 S) t' z, h6 g% l6 K9 ?: Y P
/ X% w& u2 e) [0 Y. f5 Y( f! [+ q. w
4 z9 u# l+ g2 [2 W C( l( }: d
//************2.查寻上传图像信息************
% D2 T8 _- D) F7 l: J, m, _
$url = "http://japi.juhe.cn/image_recognition/findAll";
( |* Y4 f- r+ a" }
$params = array(
7 Y6 x* `6 r) O0 O
"key" => $appkey,//APP Key
& r, Z6 Y% G3 D8 X# {
);
) T- y# J4 X& P& y+ [
$paramstring = http_build_query($params);
7 H" k! u$ L+ e; u! x
$content = juhecurl($url,$paramstring);
% s* i# h# ?! U- ~! ]
$result = json_decode($content,true);
7 |% S; Y6 X0 _0 z) A4 q
if($result){
! M+ @- E; t! ?3 g8 C9 ?( j
if($result['error_code']=='0'){
0 M, q/ n$ V. i
print_r($result);
9 b" Z0 G) Q, Q* Q) c8 @3 n- u
}else{
* E5 [5 p& \, i3 g" E- o7 c% t l
echo $result['error_code'].":".$result['reason'];
- L" g- T( i x8 `/ c) D
}
$ d7 t. D( k& q% ?
}else{
. D8 s" F" l' o d
echo "请求失败";
6 E( R; N* n' P) `
}
7 n6 U+ r8 X0 c
//**************************************************
: I( @# k7 w1 t8 k4 Z1 _' w4 B
2 F9 [& G9 J* k; D
6 L$ H7 q' C$ t. P: C7 Q
6 E f* U# [) X4 E- n; f
" e+ `, h/ B" c( d# C" {# |
//************3.删除图像信息************
/ Z; V% M% w- x0 z
$url = "http://japi.juhe.cn/image_recognition/delete";
) q2 k A+ ^+ Y# D- {, C- D" ?
$params = array(
% o1 t/ g+ B. l: j& o8 ?* O
"key" => $appkey,//APP Key
( c* F. y4 i6 \. e: \* j
"imageId" => "",//图片ID
# d( ~; `- m$ @/ Y5 K. m
);
8 x9 O: u6 w5 h3 E$ M
$paramstring = http_build_query($params);
) X. {' w3 N l4 ?4 q) h# ~ x: r
$content = juhecurl($url,$paramstring);
" w; G' w5 J# z& f( T. L0 `: ]. n! G! y
$result = json_decode($content,true);
! S9 P L. I, [6 H* {
if($result){
2 C2 y7 ], c' D7 R
if($result['error_code']=='0'){
' D7 K, O# U/ \& i) r
print_r($result);
2 p9 l X1 }( i$ N; v! @% K
}else{
) M$ m! T) V3 ~* i2 [+ H
echo $result['error_code'].":".$result['reason'];
/ M- D7 X. ^& I! w' S. q4 A* c) a* o
}
" W# X0 m2 H5 ]# @( O, O, S
}else{
7 v8 P# }& t8 X, x0 s0 `
echo "请求失败";
# s% k' `4 y2 L7 L" Q7 Y
}
1 Z) R; ?. E. Z
//**************************************************
0 H' G, J( a: J+ F
7 K- L0 J* \' N* y4 J
0 d9 K5 U2 n z W$ X8 s
! c8 t2 L7 {8 I4 ]( b
( v- G6 `3 }: r
//************4.图像识别************
5 E, [- w$ a& t( E* `
$url = "http://japi.juhe.cn/image_recognition/check";
: i/ \7 `% p8 O; V8 o/ N( p2 A
$params = array(
; I: b7 @2 j1 |& B
"file" => "",//上传的图片
1 X0 y1 f8 S/ M6 O
"key" => $appkey,//APP Key
' P4 e8 G& j: ?" k) t# S
"pname" => "",//包名
# b5 F' b, a* |2 e& ^2 R j
"device_name" => "",//设备名称(如android OS或iphone OS)
1 r: |- T3 c# |
"device_version" => "",//设备版本(如:4.0.3)
9 l7 U7 M) }2 Z$ X
"latitude" => "",//纬度
2 D( }% a5 O. p( ]
"longitude" => "",//经度
) L! ^- b; j# K8 n! a& o
"uuid" => "",//设备id
9 }0 ?) z2 F% \' a u/ ]
"zone" => "",//请求地区
9 c1 K; T$ _ T' R: ^/ o, y
);
4 b/ i9 A$ y+ r* `2 t( q d
$paramstring = http_build_query($params);
" e9 B0 }, c. h4 d
$content = juhecurl($url,$paramstring,1);
& p7 q, Y4 g# f9 i5 x; c7 V
$result = json_decode($content,true);
4 p7 r7 O4 g. G! c' R# Q f
if($result){
% U# X2 N+ ^2 S' h+ ~: C" C
if($result['error_code']=='0'){
) v" k* M0 o* @1 S1 n+ @
print_r($result);
; U3 _0 C) V' [$ A8 u
}else{
' X+ b/ f/ |- n2 u
echo $result['error_code'].":".$result['reason'];
' A8 M6 d) q5 a$ I$ N2 F1 e6 X( N9 Y
}
7 }8 z/ ?1 i8 Z7 i
}else{
/ m9 g* N/ ?- t3 e* I$ Q% O, Z
echo "请求失败";
' w6 m. `' r% ^5 ` o4 D5 M
}
& N+ A# b0 a% x" r* f6 x1 ^" W
//**************************************************
: r+ p+ E9 j E8 f$ V
& Y; x+ X$ t6 w& n4 ]& Z$ T
9 r: O# W8 J& r
; S( @2 @% p0 d. j6 I8 R1 Q
3 B: s0 {1 `- ~; x4 V* O7 q
, j4 ?2 N4 d$ W" T" q# u
/**
+ ^/ B& t0 B) y7 I- K2 Y5 N
* 请求接口返回内容
: I' k: n" l3 }9 P' W
* @param string $url [请求的URL地址]
/ d; o9 G4 o: H7 v) a4 \
* @param string $params [请求的参数]
$ b9 s y5 b$ }( _: j+ m. ~- p4 y
* @param int $ipost [是否采用POST形式]
% e; H4 s2 v+ d2 t+ R+ w
* @return string
6 l4 B$ h* s( m
*/
: [6 |9 ~- @9 ^! O, {9 V! p0 e+ P
function juhecurl($url,$params=false,$ispost=0){
2 \; `' J. H& L$ b1 Y& M3 h+ r
$httpInfo = array();
' p% |1 ?" l1 R; v
$ch = curl_init();
0 v* H5 F5 @% Y( X/ S. n# J# t
3 ^' o1 I4 h$ f; W6 g6 G8 n/ d: t, N
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
7 Z4 d- ]4 N B% g/ q! n9 V9 G
curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
' \) A' J" B& s i
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
- C0 H% x; V X
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
" P n( ]- y- @! C! _& x! h3 F
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
3 b4 b6 j, N; R$ R
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
2 h# _* ?$ `! ^6 i0 ~) W G7 L
if( $ispost )
2 |. k" W3 \5 c/ H, W& I
{
) e- ~2 J( u, D( U! V$ T
curl_setopt( $ch , CURLOPT_POST , true );
# X) a" e T( r: C
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
3 j: ^ W1 o; N1 N2 U R
curl_setopt( $ch , CURLOPT_URL , $url );
% G* E; z1 l; d4 P4 z; @- k: k
}
: x: ]- F/ R1 D% U- m1 i
else
1 V; |% H2 u7 M, U
{
9 Z1 T) o' |5 V) V
if($params){
; u4 e8 D( c3 N* Q6 ?
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
% ]5 w/ j1 G* \
}else{
0 e! [6 a% O4 M$ G9 w9 R1 q
curl_setopt( $ch , CURLOPT_URL , $url);
- y% r* P9 e* i+ v1 O; z& [# u
}
# l6 A c* J% T$ d4 g
}
, \2 K' v) @! A; ]
$response = curl_exec( $ch );
8 V, x. ?5 u" F4 w
if ($response === FALSE) {
8 {$ v2 W$ K7 j1 X% @5 K7 _: S
//echo "cURL Error: " . curl_error($ch);
1 M) ?, S6 {3 X5 u* d Y4 F
return false;
. ^- W: f1 Z6 |
}
2 p7 a- _) W! ?/ ?5 L7 j. v# x8 A Q4 L
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
; L% e9 v; S2 @, [
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
$ H% g5 I! P4 c. o
curl_close( $ch );
7 v- f; w( M, [. X$ e L6 h
return $response;
# v/ M0 j6 I8 H# k3 W
}
复制代码
2 I6 t: P" C- D. w" d( I& z8 P
. i* `1 q- r/ w/ J
欢迎光临 cncml手绘网 (http://cncml.com/)
Powered by Discuz! X3.2