dongwuxie5112 2018-08-02 19:21
浏览 321
已采纳

JSON编码到PHP变量通过Curl失败并返回空响应

i have searched a lot on google and here but im not getting any further with my problem. 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

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

  • 写回答

2条回答 默认 最新

  • dongtao4319 2018-08-04 06:14
    关注

    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.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?