我在google和此处进行了很多搜索,但是我的问题没有得到进一步解决。尽管我尝试将JSON解析为PHP变量,但我不是编码员,但是我得到一个空响应,在此我希望显示一个表或至少显示任何jsondata
这是我的代码:
<!DOCTYPE html>
<html>
<body>
<h1>Available Agents </h1>
<?php
$url = 'https://url/livewebservice/imoscontactagentstate?Username=username&Pwd=password&Cmd=GetState&ResultType=JSON';
// Initiate curl
$ch = curl_init ($url);
$data = json_encode ($data,true);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array (
' Content-Type: application/x-www-form-urlencoded ',
'charset=utf-8')
);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
var_dump(json_decode($result, true));
print_r($result);
foreach ($result as $key => $value)
{
echo ' <td><font face="calibri"color="red">'.$value[type].' </font></td><td><font face="calibri"color="blue">'.$value[category].' </font></td><td><font face="calibri"color="green">'.$value[amount].' </font></tr><tr>';
}
echo "</tr></table>";
?>
</body>
</html>
我很感谢任何提示
答案 0 :(得分:0)
据我所记得,尝试从true
中删除$data = json_encode ($data,true);
,true
仅在json_decode中用于创建关联数组
答案 1 :(得分:0)
问题已解决,我的用户名没有访问数据的权限,我们对代码进行了较小的更改,因此看起来像这样:
php
$data = array(
"UserName" => "Username",
"Pwd" => "Password",
"Cmd" => "GetAgentStateList",
"ResultType" => "JSON",
);
$url='https://host/livewebservice/service/?'.http_build_query($data);
echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/x-www-form-urlencoded", "charset=UTF-8",));
$result = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
curl_close($ch);
$f_result=json_decode($result);
print_r($f_result);
?>
我希望有一天能对来自Google的人有所帮助。