JSON 通过 Curl 编码为 PHP 变量失败,响应为空

JSON Encode To PHP Variable via Curl fails with empty response

我在 google 和此处进行了很多搜索,但我没有进一步解决我的问题。我不是编码员,虽然我正在尝试将 JSON 解析为 PHP 变量,但我得到一个空的响应,我希望在其中显示 table 或至少任何 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>

感谢任何提示

据我所知,尝试从 $data = json_encode ($data,true); 中删除 true true 仅在 json_decode 中用于创建关联数组

问题已解决,我的用户名没有访问数据的权限,我们对代码做了一些小改动,如下所示:

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 的人有所帮助。