喵-见缝插针 2017-01-18 19:01 采纳率: 0%
浏览 98

从php中的URL获取json

I understand this is a common question.I want to know how do I fetch JSON from url in php.

the below file is where the .php file is :

<html>
<head>
    <?php
    // $url = "https://localhost:8666/web1/popupData/dataWeekly.php";
    // $emp = file_get_contents($url);

    $json_url = "https://localhost:8666/web1/popupData/dataWeekly.php";
    $json = file_get_contents($json_url);
    $emp = json_decode($json, TRUE);



    ?>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>

     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">

    <script type="text/javascript">
    $(document).ready(function(){
        var JSON = <?php echo json_encode($emp); ?>;

        $('.details').click(function(){
            var name = $(this).attr('id');

            $('#dialog').html('<p>Name : ' + name + '</p> <p>Dept : ' + JSON[name]['dept'] + '</p> <p>Age : ' + JSON[name]['age'] + '</p>');
            $('#dialog').dialog();
        });
    })
    </script>
</head>
<body>

<!-- Dialog Box -->
<div style="display: none;" id="dialog"></div>
<!-- Dialog Box -->

<?php
    foreach($emp as $key => $record)
    {
    ?>
        <ul id="<?php echo $key ?>" class="details">
            <li>Name: <?php echo $key ?></li>
            <li>Dept: <?php echo $record['dept'] ?></li>
        </ul>
    <?php
    }
?>

</body>
</html>

this is the current errors that I am getting as I run the above php file :

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in C:\xampp\htdocs\web1\popupData\mp.php on line 8

Warning: file_get_contents(): Failed to enable crypto in C:\xampp\htdocs\web1\popupData\mp.php on line 8

Warning: file_get_contents(https://localhost:8666/web1/popupData/dataWeekly.php): failed to open stream: operation failed in C:\xampp\htdocs\web1\popupData\mp.php on line 8

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\web1\popupData\mp.php on line 39

please help.

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33724059 2017-01-18 19:09
    关注

    Hi you can use either cURL or file_get_contents() to load data from URL

    but if your using file_get_contents() then you must be enabled allow_url_fopen in your system & it's great for simple GET requests

    cURL

    cURL make http request just like opening an url in your browser

    try using cURL

    $json_url = "https://localhost:8666/web1/popupData/dataWeekly.php";  
    $crl = curl_init();
    curl_setopt($crl, CURLOPT_URL, $json_url);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, FALSE); 
    $json = curl_exec($crl);
    curl_close($crl);
    $emp = json_decode($json, TRUE);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器