PHP调用WebAPI接口

该接口传入参数是JSON格式,传输方式是POST,用PHP调用WebAPI接口,获取返回参数。

推荐一个Php在线测试工具,地址:https://www.dooccn.com/php/

 

 源码:

<?php
$Url = "http://218.29.74.138:17252/api/Users/GetUserInfo";//接口地址
$sl_data=array(
'UserCode'=>'00501712204701',
'MeterAddr'=>'501712204710',
'MeterType'=>'16'
);//接口输入参数
$data_str = json_encode($sl_data);//JSON格式化
$header=array(
'Content-Type: application/json',
'Content-Length: '.strlen($data_str),
);//Header参数
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");//post方式
curl_setopt($ch, CURLOPT_URL, $Url);//接口地址
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);//输入参数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//是否有返回值
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//头部参数

$data = curl_exec($ch);//返回参数
echo $data;//打印输出参数 
//var_dump($data);
?>

posted @ 2020-07-23 10:14  那一抹的温柔  阅读(1238)  评论(0编辑  收藏  举报