doubipiao1611 2017-03-10 23:20
浏览 71
已采纳

为什么我的API的JSON响应没有在PHP中显示?

I need to display the JSON response received from an API call. But due to some issues it doesn't display the response. Server/IP settings, API status are everything OK at API provider's end. So now, I couldn't figure out the issue.

I'm doing the PHP part in the following way:

PHP

$url="https://api.domain.com/getdata.php?format=json&token=2455445444&type=ROWS";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


    $output = curl_exec($ch);
    curl_close($ch);    
    $json = json_decode($output, true);

    $resCode = $json['resCode'];
    $resText = $json['resText'];

    $rows = $json['data']['ROWS'];


    if (count($rows) > 0) {

                        echo "<table border='1' width='50%' align='center'><thead><tr>
                        <th>Amount</th>
                        <th>Detail</th>
                        <th>Validity (days)</th>
                        <th>Talktime</th>
                        </tr></thead><tbody>";
                        foreach($rows as $key => $value)
                        {
                            echo " <tr><td>" .$value["column1"] . "</td> <td>" .$value["column2"] . "</td> <td>" .$value["column3"] . "</td> <td>" .$value["column4"] . "</td> </tr>";
                        }
                        echo "</tbody></table><br/>";
                }
                echo "Not available <br/> Try again!";

JSON Response from API

{
    "data":{

        "ROWS": [
                    {
                        "column1":"123",
                        "column2":"456",
                        "column3":"789",
                        "column4":"054"
                    },
                    {
                        "column1":"775",
                        "column2":"545",
                        "column3":"647",
                        "column4":"745"
                    },
                    {
                        "column1":"440",
                        "column2":"544",
                        "column3":"745",
                        "column4":"999"
                    }
                ]
        },

    "resCode":"200",
    "resText":"SUCCESS"

}

Getting Output:

Not available  
Try again!

Expecting Output in HTML

<table border="1" width="50%" align="center">
<thread>
<tr>
  <th>Column 1</th>
  <th>Column 2</th>
  <th>Column 3</th>
  <th>Column 4</th>
</tr>
</thread>
<tbody>
<tr>
  <td>123</td>
  <td>456</td>
  <td>789</td>
  <td>054</td>
</tr>
<tr>
  <td>775</td>
  <td>545</td>
  <td>647</td>
  <td>745</td>
</tr>
<tr>
  <td>440</td>
  <td>544</td>
  <td>745</td>
  <td>999</td>
</tr>
</tbody>
</table> 

Any help will be appreciated! Thanks.

  • 写回答

1条回答 默认 最新

  • dor2p0520 2017-03-13 11:20
    关注

    I used curl_error($ch) to know if there's any error with curl. And it let me know that I'm facing the issue because of SSL validation issue with the API URL.

    So, I removed the s from https in the API URL. And it worked!

    I know its a security compromise! But the referrer url is using http, so I need to remove the SSL validation from api url too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形