<?php
/**
* PHP发送Json对象数据
*
* @param $url 请求url
* @param $jsonStr 发送的json字符串
* @return array
*/
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( /*设置请求头*/
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($curl);
print_r($result,true);
curl_close($curl);
return $result;
}
$url = "http://223.99.141.141:10110/sdiot/cmiotopen/qryGPRSRealTimeInfo";
$data = json_encode(array('numType' => 1, 'queryNum' => 1064848493011, 'provinceid' => 531));
$method='POST';
echo CallAPI($method,$url, $data);
转载于:https://www.cnblogs.com/sq45711478/p/7699047.html