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);
?>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM