【问题标题】:Return only php data without html tags只返回没有 html 标签的 php 数据
【发布时间】:2020-06-23 05:04:29
【问题描述】:

我正在尝试从 shields.io 创建一个自定义屏蔽。我尝试了使用json 创建端点的路线,但由于可访问性问题,这对我不起作用。我想出了一个解决方法,我从我的 php 端调用 shields.io 链接,然后将其返回到一个端点,然后从我的md README 文件中的<img> 标签中调用该端点。

如果我在浏览器中输入网址,它可以正常工作,并且我看到了防护罩。如果我尝试在我的 README 中的 <img> 标签中使用该 url,它就不起作用。我意识到这是因为我从我的 php.ini 中返回了额外的 <html> 元素。这是我的代码:

php:

$router->get('/badge', function (AssetsManifestGateway $assetsManifestGateway) {
  $appVersion = $assetsManifestGateway->getAppVersion();

  $shield = file_get_contents("https://img.shields.io/static/v1?label=". config('app.partner_code'). "_". config('app.env'). "&message=". $appVersion);
  return $shield; 
});

自述文件:

<img src="http://<my url>/badge">

在我的浏览器中访问 url 时的响应:

<html>
 <head>
 </head>
 <body>
  <svg xmlns="http://www.w3.org/2000/svg">
   <extra content>
  </svg>
 </body>
</html>

所以我基本上只想返回该响应的&lt;svg&gt; 部分。这可能吗?

【问题讨论】:

  • 你试过使用 strip_tags 吗?
  • 是的,但这并没有起到作用。
  • 尝试使用 html_entity_decode 我希望它运作良好。
  • 不,仍然没有。我认为返回的 url 可能会返回“干净”,没有 html,但可能 html 是在其他地方呈现的。但是我查了一下,没看到。
  • 也许有办法告诉php 只发送响应而不生成任何其他html?

标签: php html shields.io


【解决方案1】:

您的问题

通过从 shields.io (file_get_contents) 获取徽章并返回 svg 徽章,您实际上使您的服务器相信您返回的是 html(请参阅 &lt;svg&gt;&lt;/svg&gt; 标签)。

我尝试了使用json 创建端点的路线,但由于可访问性问题,这对我不起作用。

不久前我也遇到过类似的问题。我通过使用 php 创建屏蔽端点解决了它。

这样你就可以使用 php 创建 JSON 并让 shields 服务器从中创建一个徽章 - 而不是从 shields.io 获取和返回徽章(你做了什么)。

回答您问题的代码

我总是使用这个函数来创建我的徽章,因为抽象在这里看起来非常有用:

function createBadgeJson($label, $message, $color="green") {
  return "{
\"schemaVersion\": 1,
\"label\": \"$label\",
\"message\": \"$message\",
\"color\": \"$color\"
}";
}

要创建您在上面尝试创建的徽章,请将其放入您的 php 文件中:

$appVersion = $assetsManifestGateway->getAppVersion();

echo createBadgeJson(config('app.partner_code'), $appVersion);

您可以像这样在 ma​​rkdown 中使用您的徽章:

![your badge](https://img.shields.io/endpoint?url=https://your-endpoint-domain.com/your-badge-path)

在 HTML 中,使用上面的链接格式作为图像源。


资源

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多