Return 只有 php 数据没有 html 标签

Return only php data without html tags

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

如果我在浏览器中输入 url 它工作正常并且我看到了盾牌。如果我尝试在我的 README 中的 <img> 标签中使用 url 它不起作用。我意识到这是因为我从我的 php 中 returning 了额外的 <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>

所以我基本上只想 return 该响应的 <svg> 部分。这可能吗?

您的问题

通过从 shields.io (file_get_contents) 获取徽章并返回 svg 徽章,你实际上让你的服务器相信你返回的是 html(见 <svg></svg> 标签)。

I tried the route where I create an endpoint with json but that won't work for me because of accessibility issues.

我刚才也遇到过类似的问题。我通过 使用 php 创建一个屏蔽端点来解决它。

这样你就可以使用 php 创建 JSON 并让盾牌服务器从中创建徽章 - 而不是从 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);

您可以像这样在 markdown 中使用徽章

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

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


资源