I am trying to create a custom shield from shields.io. I tried the route where I create an endpoint with json
but that won't work for me because of accessibility issues. I came up with a workaround where I call the shields.io link from my php side and then return that to an end point and then in return call that end point from my md
README file in my <img>
tags.
If I enter the url in the browser it works fine and I see the shield. If I try to use that url in my <img>
tags in my README it doesn't work. I realized this is because I am returning extra <html>
elements from my php. Here is my code:
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;
});
README:
<img src="http://<my url>/badge">
Response when accessing the url in my browser:
<html>
<head>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg">
<extra content>
</svg>
</body>
</html>
So I basically want to return only the <svg>
part of that response. Is this possible?
By fetching the badge from shields.io (file_get_contents
) and returning the svg badge, you actually cause your server to believe that what you are returning is html (see the <svg></svg>
tags).
I tried the route where I create an endpoint with
json
but that won't work for me because of accessibility issues.
I had similar issues a while ago. I resolved it by creating a shields endpoint with php.
That way you create JSON using php and let the shields server create a badge from it - instead of fetching and returning badges from shields.io (what you did).
I always use this function to create my badges, as abstraction seems very useful here:
function createBadgeJson($label, $message, $color="green") {
return "{
\"schemaVersion\": 1,
\"label\": \"$label\",
\"message\": \"$message\",
\"color\": \"$color\"
}";
}
To create the badge you tried to create above, put this into your php file:
$appVersion = $assetsManifestGateway->getAppVersion();
echo createBadgeJson(config('app.partner_code'), $appVersion);
You can use your badge in markdown like this:

Or in HTML by using the link format from above as an image source.
Resources