cncml手绘网
标题:
图像识别API调用代码实例(PHP)
[打印本页]
作者:
admin
时间:
2018-7-8 00:13
标题:
图像识别API调用代码实例(PHP)
提供B2B的移动商务解决方案,通过图像识别技术,通过摄像头扫描直接查询和购买商品。
; H, p; d3 G+ Y2 y7 v3 o
1.添加商品信息
0 i+ n8 k, Q( s, ?& b: g
2.添加图片
: |" G' d- [: ?: ^8 W
3.查询所有产品
, g! p$ ?8 P& {
4.查询某个具体产品信息
/ p5 ?1 i7 C& ?$ r' {) A
5.删除某个产品
! R) b3 h* \8 }6 M
6.删除图片
/ S7 [1 s+ P+ _% p* r- ?, ~
7.图像识别
# |# r; O& |; T1 T
: X0 t( a" a1 m8 @) P3 E
图像识别API文档:
https://www.juhe.cn/docs/api/id/117
,申请后生成AppKey
" q4 j2 h, \9 y- Q
" A( K! H1 X0 R7 a5 D3 ?3 Y- {5 _/ c
PHP示例:
9 d4 n3 s$ A; k" B; {/ I+ g
<!--?php
* r6 b" h& _+ e! r% I( I
// +----------------------------------------------------------------------
+ M: ^+ X( n% m, Y/ r& u
// | JuhePHP [ NO ZUO NO DIE ]
, u) _- c$ e# {5 C
// +----------------------------------------------------------------------
1 u! {" z2 Z) f0 w j5 J
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
0 B4 W1 N2 ]8 L7 l- n8 B# j
// +----------------------------------------------------------------------
; J0 \" X# v$ _
// | Author: Juhedata <info@juhe.cn-->
- _0 l. X, y6 C0 Y G4 D5 g( G5 K; |8 I
// +----------------------------------------------------------------------
; Q6 Q6 }6 m+ w) c9 i6 G! g1 h
* ~# ^4 \& n% t! [
//----------------------------------
& d8 h! W1 k. a& u& J; ` g% v- e' L
// 图像识别调用示例代码 - 聚合数据
7 {* N9 P4 B( A
// 在线接口文档:http://www.juhe.cn/docs/117
1 o& q5 f0 @2 B) C3 J
//----------------------------------
. i; y% J3 `" [; l
% R% Q. ]# ]! E5 l' v$ }# O0 U
header('Content-type:text/html;charset=utf-8');
4 F+ _' q. }& s, P) C/ j3 U
% \! w' o# q- s- J: Y9 }" [" I
% K; T8 F) j" m0 N, L7 d5 Q
//配置您申请的appkey
' T4 `- s: q& ?& @
$appkey = "*********************";
7 u; |" W. J+ y$ p, d$ e
+ M3 H/ N. V" j" O
1 |" \! j% S1 M" @, a( s
; u) E3 X5 j8 _
* J8 y3 x y$ A, V7 ^. K4 K1 X
//************1.图像上传************
: L: o+ {1 V. |2 a+ o5 e
$url = "http://japi.juhe.cn/image_recognition/upload";
* S+ q- q1 }) v f
$params = array(
3 k+ b4 u" C. M6 u6 s
"file" => "",//上传的单张图片
/ X4 v$ i1 K5 e& L. E7 Q9 A
"key" => $appkey,//APP Key
+ r4 E/ A Z7 B: M
"pname" => "",//包名
, U- G" q( L0 r8 \
);
4 Z1 y1 y2 e9 ^3 r/ h3 Z
$paramstring = http_build_query($params);
( n" I; R8 |3 O4 z* P) r9 p
$content = juhecurl($url,$paramstring,1);
; g) G/ h/ Y" x$ E- K8 o
$result = json_decode($content,true);
Q* ]& M. g x2 n
if($result){
. _: p- E8 K4 U+ |* X5 I
if($result['error_code']=='0'){
" T# {0 `8 i$ _3 y
print_r($result);
1 x2 G) Z) x! l0 G8 a4 k7 Y
}else{
' b8 O: V4 _8 ]$ }$ K
echo $result['error_code'].":".$result['reason'];
! M" A- \7 X% P# C4 Q W" I+ \2 j( q
}
- L( `1 t0 A) _
}else{
6 \; M0 K) X" ^: W
echo "请求失败";
; h' t! N7 d: z0 [* z: Q/ C
}
. N- K/ z3 c8 h7 ]
//**************************************************
. E a" W3 V1 Z3 W
# J- z& q9 Q' Y4 B- N% @) ^4 V
7 k- u' Z! z4 N1 l7 D
6 [, r9 s) a7 D, E5 ]) g
+ M0 b% U0 _% k0 j. J
//************2.查寻上传图像信息************
, S# T8 w4 G) Q; J+ \8 t# D
$url = "http://japi.juhe.cn/image_recognition/findAll";
- ~5 a' q4 q6 L, ?* R
$params = array(
3 Z$ |; L# u7 S! |. q
"key" => $appkey,//APP Key
! O {- @' \. y! v
);
" ]: `: @9 l) T- l! L i: y
$paramstring = http_build_query($params);
, f/ c& T( ~9 c! l% }$ H
$content = juhecurl($url,$paramstring);
3 s. ^* c( v9 x: I1 q
$result = json_decode($content,true);
t1 N1 v5 g# ^, T
if($result){
: r! a8 u) z- U: ?7 r
if($result['error_code']=='0'){
7 U- I! t+ T, }* v: n
print_r($result);
, S0 J% ]6 |$ m! e
}else{
; W' x3 }: D. A, c& a4 W
echo $result['error_code'].":".$result['reason'];
: v+ B# z8 @3 o: u1 H+ t4 a( B1 O
}
" O+ p. y3 @9 |5 [
}else{
8 Z* G5 y* Y7 n3 `/ q6 ]: m
echo "请求失败";
9 B2 Z( o- z- x% w
}
) D& \+ r" n; v. z3 j- b
//**************************************************
7 n1 P# i' ?1 ?. F
; z1 V3 p2 P9 X) a5 n
- g& {! r( {0 n, t( U
8 P! I- E7 V; H. @5 ~+ H4 B
( B0 J) d: L: ^& [' z6 m' _
//************3.删除图像信息************
' T' S0 E" t3 w" C, G# t
$url = "http://japi.juhe.cn/image_recognition/delete";
$ i& l' K! x4 S1 ~) U% y: I
$params = array(
. r# V" u) ]& B& `9 b8 t
"key" => $appkey,//APP Key
3 _( @- y/ l7 C% w" R
"imageId" => "",//图片ID
# f6 S0 B, H3 x5 ?2 I: q/ l) p3 B
);
3 L% e6 B3 r# k4 i) t' F5 Y8 y/ a7 e
$paramstring = http_build_query($params);
3 W$ J0 S4 G( z: F. ^# l k- H" J
$content = juhecurl($url,$paramstring);
, t9 q, z' m) z) H3 G# ^
$result = json_decode($content,true);
2 T. j; Z& E/ q/ a8 s
if($result){
5 ~% A! g; E/ P
if($result['error_code']=='0'){
4 {2 w9 S' K C3 Q- ^
print_r($result);
& G% m5 b: K8 J& D' ?% }% S7 D2 n( r
}else{
9 R4 J( O) y+ d
echo $result['error_code'].":".$result['reason'];
! g0 o( p8 w) ^7 C; j3 e! X
}
( o6 k5 F- ~! p: Q7 h9 c1 q
}else{
& y' V! M" X6 Y
echo "请求失败";
% U8 U( ~( Q6 m
}
* W5 _. q5 Z m8 [4 E& `5 E
//**************************************************
0 H0 }! {+ J3 k
2 @* T9 W6 I1 d
3 I, e2 F: p+ D$ T, S
, p6 P) i y6 _0 b, \+ c
1 g/ S5 Z2 i/ z1 }- y
//************4.图像识别************
3 q* h) q3 x6 r5 l( c
$url = "http://japi.juhe.cn/image_recognition/check";
9 b9 T/ o* `! a0 e7 y$ i) ?6 E
$params = array(
0 r* G+ k, Y" j: v
"file" => "",//上传的图片
1 O) }! w- ?8 v$ X- l5 h; i
"key" => $appkey,//APP Key
9 }$ m9 X+ o. U! P
"pname" => "",//包名
; A s2 B: G7 g, @1 w
"device_name" => "",//设备名称(如android OS或iphone OS)
3 T5 g: u7 P A, K
"device_version" => "",//设备版本(如:4.0.3)
) ^0 r4 s" R* j( y, q
"latitude" => "",//纬度
2 c; k; C. P1 A. P& z- e
"longitude" => "",//经度
9 ?+ F6 a6 J& M. W3 h
"uuid" => "",//设备id
. N' p/ t. f7 K# u* H/ E3 M( Q
"zone" => "",//请求地区
; J% x i8 v# x. B- i9 f m$ T
);
* e3 j6 y6 P, T: _9 r {
$paramstring = http_build_query($params);
7 h2 ]! x. v9 m$ z
$content = juhecurl($url,$paramstring,1);
! [( ^6 B8 X2 r+ C# `; a
$result = json_decode($content,true);
* o0 O- ?6 F% n9 O! h
if($result){
( V9 G' e% D) }/ i% @
if($result['error_code']=='0'){
6 L% C( a0 j9 W- t) \7 B
print_r($result);
/ e; F7 U+ M( C1 P6 `7 a7 I3 o) E
}else{
; R' {- L6 N X
echo $result['error_code'].":".$result['reason'];
/ @6 L. H4 }( A. O W: S: [
}
2 K; c$ v4 @. s) i6 c; [$ j# E
}else{
: B$ [; \4 E; Q' u& d3 H
echo "请求失败";
8 l* @6 ~# q/ v @
}
' q0 b g/ {, w% [3 y- c Q8 g
//**************************************************
2 O0 m. s, ^" Q% |, G; U/ N* Z
' ?5 k; z$ ?# A" q
% L; V5 H! l* a& {' E) B6 d
' f7 Q" A$ e0 B. q$ m- u R k- U
6 F9 z1 [$ D) B; ]. H- u! [
' L9 q& `' y; n# u
/**
) e/ D: U! ]( w
* 请求接口返回内容
' h( G$ _6 v$ |. o4 C, S9 }
* @param string $url [请求的URL地址]
* X& E& D, f% K' z5 p, c0 a7 l
* @param string $params [请求的参数]
! l( M O6 o3 E4 r
* @param int $ipost [是否采用POST形式]
' R& k6 H# ]* ~
* @return string
) l3 T* \9 n, ^5 |
*/
2 Z7 |, C& @% I: r A( ~, A! x4 ~
function juhecurl($url,$params=false,$ispost=0){
6 B! ?7 I9 c. Q1 l
$httpInfo = array();
: v; a$ h; h& R6 l3 W$ m
$ch = curl_init();
2 N% B5 R; ^6 b% c
7 u. x K5 R8 B1 E) T$ w! n( ?0 a y
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
& }7 m+ s( f8 k; Y
curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
1 R4 L& J, E/ i
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
) ^- E7 n( z, }( I
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
i1 m& E8 c. g( G5 A
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
5 X( }$ }9 u2 O* K7 l
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
) @0 M9 b+ d& I% P+ W
if( $ispost )
6 Z+ V7 U/ N) Y1 z8 L
{
( j0 E Y: v& g" _- C- x
curl_setopt( $ch , CURLOPT_POST , true );
. U8 r7 C+ P3 ?% p
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
. Z7 h( I% Y7 b! y: a8 V$ T
curl_setopt( $ch , CURLOPT_URL , $url );
. g0 Z3 G8 }' d4 `1 s; x, V
}
+ D1 h8 q' H) z# @
else
$ R7 p2 R- u7 z ? [3 h, V
{
, ~' l% `- t: o8 R8 Q. a$ s# s
if($params){
7 U- a8 e3 I+ ]: z3 ~# g7 X7 j
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
m5 y1 [1 @. P, K7 i
}else{
: O0 u0 c$ U! r7 e3 k' P6 ~! s
curl_setopt( $ch , CURLOPT_URL , $url);
3 l6 o7 S, e' D3 Z. f6 T- e
}
3 y, V6 f; B# r: u; Z0 _2 ~- j% f0 S
}
# z% `& V0 v4 C6 B8 T% k7 H) O
$response = curl_exec( $ch );
8 ?0 \5 E+ E5 o6 R# j+ M: ~7 A
if ($response === FALSE) {
- Y+ f1 e, L. u6 {0 H
//echo "cURL Error: " . curl_error($ch);
/ D" l0 X" w1 ^* C7 p, Y; }
return false;
1 p% X, J% c: X( Q) Q
}
5 F4 d X3 ?! V2 o T7 j
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
' k4 X$ u# z; S$ t, h( O
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
\# }( |/ I% X
curl_close( $ch );
5 V* U6 ?' u9 m4 o5 c' q
return $response;
$ f- A7 j& ~: t' z
}
复制代码
5 O7 c; B' [2 Q: ^: G/ |# h/ W
: k% `( C" ?- R$ q7 V3 ~6 B
欢迎光临 cncml手绘网 (http://www.cncml.com/)
Powered by Discuz! X3.2