dongyin2390 2015-06-26 09:02
浏览 140

从php文件调用JSON到HTML页面

I'm making a html page that needs to show a video.The URL of the video is found in a jsondecode output. Is here any way to pick pieces from that JSON output and use it in my html page? I have tried POST and GET and echo but it doesn't seem to work.

To be clear: I'm trying to get it from a php file to a separate HTML file.

The JSON response is in a separate file in a function.

public function getURL(){
    $server_url= ilObjPresentations2GoGUI::getServerUrl();
     $secret_key= ilObjPresentations2GoGUI::getSecretKey();
     $authorized_group= ilObjPresentations2GoGUI::getAuthorizedGroup();
     $link_usable= ilObjPresentations2GoGUI::getLinkUsable();
     $query = 'technology';

    $date = new DateTime('NOW');
    $ts = $date->format('Y-m-d\TH:i:s');
    $interval = 'PT' . $this->link_usable . 'H';
    $date->add(new DateInterval('PT' . $link_usable . 'H'));
    $expired = $date->format('Y-m-d\TH:i:s');
    $url = $server_url . '?action=search&query='. $query . '&group='. $authorized_group . '&ts=' . $ts . '&expired=' .$expired; 

    $token = ilObjPresentations2GoAPI::create($url,$secret_key);
    $goodurl = $url . '&token=' . $token;

    $json = file_get_contents($goodurl);
    $the_data = json_decode($json, true);}

Output:

array(9) { ["schemaVersion"]=> string(3) "1.0" ["catalogueVersion"]=> string(14) "4.0.5651.20898" ["encoding"]=> string(5) "UTF-8" ["action"]=> string(6) "search" ["query"]=> string(10) "technology" ["page"]=> string(1) "1" ["pageCount"]=> string(1) "1" ["success"]=> string(4) "true" ["response"]=> array(3) { [0]=> array(10) { ["id"]=> string(6) "bWKUL2" ["title"]=> string(57) "Technology-Based Professional Learning and Support Models" ["date"]=> string(19) "2011-12-05T10:19:21" ["contributors"]=> string(0) "" ["thumbnailUrl"]=> string(409) "https://DEMO-FORMS.presentations2go.eu/P2G/plugins/mediaservice.aspx?

(this is not all, but otherwise it would have been half a page)

  • 写回答

1条回答 默认 最新

  • dpcj32769 2015-06-26 09:23
    关注

    You can just use the data within your JSON decoded array within your html, as long those files have a .php extension or they're included by PHP:

    <div><?php echo $the_data['title']; ?></div>
    

    Since your $the_data array is within the method getURL(), you could return the URL that's inside the $the_data array:

    public function getURL() {
        ...
    
        $the_data = json_decode($json, true);
    
        return !empty($the_data['url']) ? $the_data['url'] : '';
    }
    
    ...
    
    <div><?php echo $your_object->getURL(); ?></div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错