简体   繁体   English

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

[英]JSON Encode To PHP Variable via Curl fails with empty response

i have searched a lot on google and here but im not getting any further with my problem. 我已经在Google和此处进行了大量搜索,但是我的问题没有得到进一步解决。 I am not a coder though I am trying to parse JSON to PHP Variables, but i get an empty response, where i want a table to be shown or at least any jsondata 虽然我尝试将JSON解析为PHP变量,但我不是编码员,但是我得到一个空响应,在此我希望显示一个表或至少显示任何jsondata

Here is what my code looks like: 这是我的代码:

<!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>

I am grateful for any hints 我很感谢任何提示

Try to remove true from $data = json_encode ($data,true); 尝试从$data = json_encode ($data,true);删除true $data = json_encode ($data,true); as far as can i remember true is used only in json_decode to create an associative array 据我所记得, true仅在json_decode中使用以创建关联数组

The problem was solved my Username did not have the permission to access the data and we made minor changes to the code so it looks like this: 问题已解决,我的用户名没有访问数据的权限,并且我们对代码进行了细微更改,因此看起来像这样:

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

i hope this helps someone coming from google someday. 我希望这可以帮助某天来自Google的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM