1.封装的curl公共方法
function curlJson(
u
r
l
,
url,
url,data){ //传json post
c
h
=
c
u
r
l
i
n
i
t
(
)
;
c
u
r
l
s
e
t
o
p
t
(
ch = curl_init(); curl_setopt(
ch=curlinit();curlsetopt(ch, CURLOPT_URL,
u
r
l
)
;
c
u
r
l
s
e
t
o
p
t
(
url); curl_setopt(
url);curlsetopt(ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’)); //设置请求头,这个一定要写
curl_setopt(
c
h
,
C
U
R
L
O
P
T
P
O
S
T
,
1
)
;
c
u
r
l
s
e
t
o
p
t
(
ch, CURLOPT_POST, 1); curl_setopt(
ch,CURLOPTPOST,1);curlsetopt(ch, CURLOPT_POSTFIELDS,
d
a
t
a
)
;
c
u
r
l
s
e
t
o
p
t
(
data); curl_setopt(
data);curlsetopt(ch, CURLOPT_RETURNTRANSFER, true);
r
e
s
p
o
n
s
e
=
c
u
r
l
e
x
e
c
(
response = curl_exec(
response=curlexec(ch);
curl_close($ch);
return $response;
}
2.调用:
public function dayin(){
$url = ‘http://localhost/xxx/xxx/xxx’; //请求的路径
$string = array( //请求的参数
‘OrderType’=>1,
‘PrintContent’=>1,
‘PrintPaper’=>‘A4’,
‘CorpBillidDatas’=>array(
‘CorpBillid’ => ‘5c3fca6c71ed922e1487311d’
),
‘Verify’=>array(
‘Clientid’ => ‘758’,
‘Token’ => ‘9VdKIr2FB8Jr40XSCBRW’
)
);
p
a
r
a
m
e
=
j
s
o
n
e
n
c
o
d
e
(
parame = json_encode(
parame=jsonencode(string); //参数转成json
v
a
l
=
c
u
r
l
J
s
o
n
(
val = curlJson(
val=curlJson(url,$parame); //调用方法访问接口
return $val; //输出 =返回值
}