I am new to this stuff but I love it. I made a little REST Api using the Slim Framework. Now I would like to have an ongoing documentation about it. I think swagger is the right choice but I have not found out yet how to integrate it ?
Cheers & thanks for your patience :)
I think you are looking for this project: zircote/swagger-php
Here you'll find how to generate the doc on user request to a URL.
Basically you have to annotate your code with the Swagger Annotations, then create another route in Slim with a code similar to this:
<?php
use Swagger\Swagger;
$swagger = new Swagger('/project/root/top_level');
header("Content-Type: application/json")
echo $swagger->getResource('/pet', array('output' => 'json'));
And it will generate the Swagger API docs on the fly for you.
A short update to the answer of adosaiguas:
When using Slim Framework 4.0 and zircote/swagger-php
one can provide an api endpoint providing the swagger / OpenAPI 3.0 json description using the following code:
use function OpenApi\scan;
/**
* #OA\Get(
* path="/openapi",
* tags={"documentation"},
* summary="OpenAPI JSON File that describes the API",
* #OA\Response(response="200", description="OpenAPI Description File"),
* )
*/
$app->get('/openapi', function ($request, $response, $args) {
$swagger = scan('--PATH TO PROJECT ROOT--');
$response->getBody()->write(json_encode($swagger));
return $response->withHeader('Content-Type', 'application/json');
});
Related
I just started with php and Slim framework. My main goal is to generate a RESTful API where I have a /swagger route where I show the API documentation using swagger ui.
This are the steps I followed:
Install last version php
Install composer
Install required dependecies:
composer require slim/slim:"4.*"
composer require zircote/swagger-php
Create API project (composer create-project slim/slim-skeleton testAPI)
Anotate involved elements in my API
Generate swagger file (.\vendor\bin\openapi -output .\swagger.json .\src)
Add new route to my API:
use OpenApi\Generator as OpenApiGenerator;
/**
* #OA\Get(
* path="/openapi",
* tags={"documentation"},
* summary="OpenAPI JSON File that describes the API",
* #OA\Response(response="200", description="OpenAPI Description File"),
* )
*/
$app->get('/swagger', function ($request, $response, $args) {
$swagger = OpenApiGenerator::scan(['../swagger.json']);
$response->getBody()->write(json_encode($swagger));
return $response->withHeader('Content-Type', 'application/json');
});};
But when I go and run the api and check /swagger route this is what I get:
{
"statusCode": 500,
"error": {
"type": "SERVER_ERROR",
"description": "WARNING: Required #OA\\PathItem() not found"
}
}
Have I missed something? Or my OpenApiGenerator::scan(['../swagger.json']) does not make sense? I have seen people doing OpenApiGenerator::scan(['.']) but that gives me the exact same output.
Solved it by making my /swagger route returning the swaggerUI html. For that, it is necessary to have this folder in the project.
I am trying to make the Restful API documentation in PHP swagger, what I did before is that I changed the JSON to work out, now I know we can make the JSON by making PHP files and using swagger notation. I did check the Pet.php example and I get the code but I don't know how to execute the file to get the JSON API documentation which I can link with my Swagger UI. I read the documentation but it is so confusing and I don't know how to get through this problem can anyone help, please? Here is the link I study but to no worth.
https://github.com/zircote/swagger-php
Swagger-PHP for generating JSON file for Swagger-UI
Can anyone tell me step-by-step how to generate the API documentation in JSON? I will be very thankful to him thanks.
There are two way to achieve this in swagger-php 2.0.
I.
The first solution is to create a controller or script which will generate the documentation on each request. This is a good solution in a development environment where you want to see quickly the outcome of your changes.
Here is an example of a controller which does this.
<?php
namespace Controllers;
use Swagger\Annotations as SWG;
use Swagger;
/**
* #SWG\Swagger(
* basePath="/path/to/opration/",
* produces={"application/json"},
* swagger="2.0",
* #SWG\Info(
* version="1.0.0",
* title="My API"
* )
* )
*
*/
class Documentation {
const API_PATH = "path/to/my/documented/files/";
public function show(){
$swagger = Swagger\scan(self::API_PATH);
return json_enconde($swagger); //you can echo this in the calling script.
}
}
Note: The example above assumes you installed Swagger-php with Composer and that the calling script include the composer generated autoload file (usually called: vendor/autoload.php).
II.
The first solution consisting of generating a static json API documentation is described here: https://stackoverflow.com/a/21380432/2853903
This solution recommended for production deployment, where you would not want to regenerate the documentation on every request.
I am following this guide (http://www.lornajane.net/posts/2013/oauth-middleware-for-slim) to setup oAuth2 with php SLIM.
I dont't understand this part:
$auth = new \Service\Mysql\AuthService($this->mysql, $this->config);
$validated_user_id = $auth->verifyOAuth($authHeader);
$this->app->user_id = $validated_user_id;
Where can I take the class \Service\Mysql\AuthService and what is the variable config ?
Otherwise is there another guide with more details also without direct SLIM implementation ?
Thanks
That's the service class that will actually do the loading and storing of user details.
Then you should put your own class.
This could be useful: https://github.com/alexbilbie/oauth2-example-resource-server
This is making me crazy. I've been trying out all sorts of things but I just can't figure this out. LinkedIn's documentation is horrible...
All I need to do is simple: I need to search for a company (using a keyword) and retrieve the company id. I have issues with setting up the OAuth request and with making the request. Any advice on how to do this, especially without installing any PHP libraries?
FYI, my code: I got the OAuth.php from here.
require_once 'OAuth.php';
$base_url = 'http://api.linkedin.com/v1/company-search';
$consumer = new OAuthConsumer('mykey', 'mysecret');
$token = new OAuthToken('tokenkey', 'tokensecret');
$parameters = array (keyword => 'Apple');
$request = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $base_url, $parameters);
print_r($request);
Thanks
You're using a good OAuth library that I do recommend, but it seems to me you're missing on the Linkedin Library, or at least you haven't included the full code.
In any case, try using the simple-linkedinphp library, which uses your oauth library above. I had to use a few libraries in the past, and this is one of the best, particuarly if you're going to use Faceted search. Make sure you also check the quick start guide, it will help you a lot, as well as the class reference. The company search API function could be found here.
You could always use the raw() function for any custom calls not supported by the above function. I had to use that for some calls.
I am trying to write a short Rest Service with Zend Framework. But the documentation is not the best at this Part.
I have an ApiController extended Zend_Rest_Controller with all needed abstract methods. My goal is to get Post data and return something.
My client looks like this:
public function indexAction()
{
$url = 'http://localhost/url/public/api';
$client = new Zend_Rest_Client();
$client->setUri($url);
$client->url = 'http://www.google.de';
$result = $client->post();
}
but the provided "$client->url" is not inside the post array on Server side. Does I have to use Zend Rest Server inside the postAction on my ApiController?
If someone has an example how to send and to get the data with Zend Rest, that would be great.
Maybe this tutorial Create RESTful Applications Using The Zend Framework can help.
Have you tried setting it to access 127.0.0.1 rather than localhost? I know this can cause issues sometimes.