首页 > 解决方案 > 使用 API 获取数据时,HTML 代码未在 PHP 中解析。任何想法为什么会发生这种情况?

问题描述

因此,我编写了一个 php 脚本来使用 CURL 与 Buzzsprout(一个播客托管网站)的 API 进行通信。PHP 脚本在自己运行时运行良好,并且 API 数据被获取,但是当我在 PHP 文件中包含 HTML 代码时,代码会重新出现,而无需在浏览器中解析。控制台中没有错误日志,也没有警告。我是 API 和 PHP 的新手,不知道为什么会这样。


继承人的脚本:-出于显而易见的原因,我删除了帐户 ID 和授权令牌。

<?PHP
   header('Content-Type: application/json'); // Specify the type of data
   $ch = curl_init('https://www.buzzsprout.com/api/*(my-account-id)*/episodes'); 
   $authorization = "Authorization: Bearer *(my security token)*";
   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_HTTPGET, 1);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   $result = curl_exec($ch);
   curl_close($ch);   
   echo $result; 
?>

这会获取所需的数据并显示在浏览器中。但是,如果附加我的 html 文件,该页面只会显示 html 代码而不对其进行解析。


这是最终文件:

<?php

header('Content-Type: application/json'); // Specify the type of data
       $ch = curl_init('https://www.buzzsprout.com/api/839368/episodes'); // Initialise cURL

       $authorization = "Authorization: Bearer 6f7bfe7b6cf43bf326873d478fbfb562"; // Prepare the authorisation token
       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // Inject the token into the header
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_HTTPGET, 1); // Specify the request method as POST

       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // This will follow any redirects
       $result = curl_exec($ch); // Execute the cURL statement
       curl_close($ch); // Close the cURL connection

echo $result;

?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Babushka</title>
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <meta content="" name="keywords">
  <meta content="" name="description">

  <!-- Favicons -->
  <link href="img/favicon.png" rel="icon">
  <link href="img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,700,700i|Raleway:300,400,500,700,800" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Kelly+Slab&display=swap" rel="stylesheet">
  <!-- Bootstrap CSS File -->
  <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">

  <!-- Libraries CSS Files -->
  <link href="lib/font-awesome/css/font-awesome.min.css" rel="stylesheet">
  <link href="lib/animate/animate.min.css" rel="stylesheet">
  <link href="lib/venobox/venobox.css" rel="stylesheet">
  <link href="lib/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet">

  <!-- Main Stylesheet File -->
  <link href="css/style.css" rel="stylesheet">

  <!-- =======================================================
    Theme Name: TheEvent
    Theme URL: https://bootstrapmade.com/theevent-conference-event-bootstrap-template/
    Author: BootstrapMade.com
    License: https://bootstrapmade.com/license/
  ======================================================= -->
</head>

<body>

  <!--==========================
    Header
  ============================-->
  <header id="header">
    <div class="container">

      <div id="logo" class="pull-left">
        <!-- Uncomment below if you prefer to use a text logo -->
        <h1><a style="font-family: 'Kelly Slab', cursive; font-weight:900; font-size:22px;" href="#main"><span>Babushka</span></a></h1>

      </div>

      <nav id="nav-menu-container">
        <ul class="nav-menu">
          <li class="menu-active"><a href="#intro">Home</a></li>
          <li><a href="#contact">Contact Us</a></li>
          <li><a href="#schedule">About Us</a></li>
          <li><a href="#venue">Contributions</a></li>  
          <li class="buy-tickets"><a href="AbstractFileSubmission.php">Explore Our Stories</a></li>
        </ul>
      </nav><!-- #nav-menu-container -->
    </div>



  </header><!-- #header -->




  <a href="#" class="back-to-top"><i class="fa fa-angle-up"></i></a>

  <!-- JavaScript Libraries -->
  <script src="lib/jquery/jquery.min.js"></script>
  <script src="lib/jquery/jquery-migrate.min.js"></script>
  <script src="lib/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="lib/easing/easing.min.js"></script>
  <script src="lib/superfish/hoverIntent.js"></script>
  <script src="lib/superfish/superfish.min.js"></script>
  <script src="lib/wow/wow.min.js"></script>
  <script src="lib/venobox/venobox.min.js"></script>
  <script src="lib/owlcarousel/owl.carousel.min.js"></script>

  <!-- Contact Form JavaScript File -->
  <script src="contactform/contactform.js"></script>

  <!-- Template Main Javascript File -->
  <script src="js/main.js"></script>
</body>

</html>

忽略尚未完成的网站的 HTML。

这是我包含 HTML 代码后的屏幕截图:- HTML 包含在 PHP 中后的浏览器屏幕截图

标签: phphtmlparsingcurl

解决方案


您首先回显$result您的 PHP,然后<!DOCTYPE html>-> 开始,这是行不通的。您必须在 HTML 文档的适当位置回显$resultPHP body


推荐阅读