在html页面中获取php变量的值

时间:2016-10-25 18:54:58

标签: php html

我想访问我在HTML页面上通过PHP脚本获得的php变量

这是我的php脚本:

<?php
    $url="http://api.giphy.com/v1/gifs/search?q=funny+cat&offset=100&limit=1&api_key=dc6zaTOxFJmzC";

    $json = file_get_contents($url);
    $file = json_decode($json);
    $images = $file->data[0]->images;

    $entityUrl =  $file->data[0]->images->original->mp4;
    $entityId =  $file->data[0]->id;
?>

这是我的html页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1"/>
<title>Insert title here</title>
</head>
<body>

<?php include "start.php" ?>
 <video width="320" height="240" autoplay="autoplay">
  <source src="<? echo $entityUrl ?> " type="video/mp4" />
</video> 
</body>
</html>

但在此配置中我的代码不起作用。有任何想法吗? php文件的名称=&#34; start.php&#34; ,html文件&#34; starting_page.html&#34;,两者都在同一目录中。

2 个答案:

答案 0 :(得分:2)

让自己更轻松,只需制作一个.php文件:

<?php
  $url="http://api.giphy.com/v1/gifs/search?q=funny+cat&offset=100&limit=1&api_key=dc6zaTOxFJmzC";

  $json = file_get_contents($url);
  $file = json_decode($json);
  $images = $file->data[0]->images;

  $entityUrl =  $file->data[0]->images->original->mp4;
  $entityId =  $file->data[0]->id;
?>
<!DOCTYPE html>
 <html>
  <head>
   <meta charset="ISO-8859-1"/>
   <title>Insert title here</title>
  </head>
  <body>
   <video width="320" height="240" autoplay="autoplay">
    <source src="<?php echo ''.$entityUrl.''; ?> " type="video/mp4" />
   </video> 
  </body>
 </html>

然后你不必乱用htaccess / rewrites

答案 1 :(得分:1)

在您的文件夹(或您网站的根文件夹)中创建.htaccess文件并添加以下行:

  • 如果您使用的是Apache2 @ Ubuntu / Debian:

    AddType application/x-httpd-php .html .htm

  • 如果您将PHP作为CGI运行:

    AddHandler application/x-httpd-php .html .htm

并在你的html文件中替换:

<? echo $entityUrl ?>

使用:

<?= $entityUrl ?>

<?php echo $entityUrl; ?>