【问题标题】:Read and store page output into another page?读取页面输出并将其存储到另一个页面中?
【发布时间】:2014-01-09 01:48:03
【问题描述】:

我有一个 PHP 页面,它根据其 GET 回显 JSON 数据。示例:

http://localhost/htdocs/includes/person_get.php?id[]=1&id[]=2

[{"id":"1","name_first1":"Daniel","name_last1":"Pendergast"},{"id":"2","name_first1":"Dennis","name_last1":"Pendergast"}]

我想要做的是从另一个页面读取它并解码成一个数组。我该怎么做?

【问题讨论】:

    标签: php json


    【解决方案1】:

    我会亲自提取将person_get 中的人检索到一个函数中的逻辑,并将该函数包含在person_get 和需要使用该逻辑的任何其他页面中。

    如果您一心想要调用它,您可以使用file_get_contentsjson_decode

    $data = json_decode(file_get_contents('http://localhost/htdocs/includes/person_get.php?id[]=1&id[]=2'));
    

    https://stackoverflow.com/a/2100310/2033671

    【讨论】:

    • 谢谢。我这样拆分它的原因是,我还希望能够通过 AJAX 在 Javascript 中访问它。你能想出一种适用于 PHP 和 JS 的方法吗?
    【解决方案2】:

    看看jQuery和jQuery函数$.load()$.get()

    使用 jQuery (javascript),您可以执行以下操作:

    $.get(
        'http://localhost/htdocs/includes/person_get.php', 
        { "id[]": [1, 2] }, 
        function( data ) {
            // data is your result array
        }
    );
    

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 1970-01-01
      • 2019-07-20
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多