Run the following commands in the directory where you want to create a new project.
Bash
composer init
Project configuration
In case you're adding a Kiota client to an existing project, the following configuration is required:
composer.json > require > php set to "php": "^8.0 || ^7.4" or later.
Add dependencies
Before you can compile and run the generated API client, you need to make sure the generated source files are part of a project with the required dependencies. Your project must have a reference to the abstraction package. Additionally, you must either use the Kiota default implementations or provide your own custom implementations of the following packages.
Add --exclude-backward-compatible
if you want to reduce the size of the generated client and are not concerned about
potentially source breaking changes with future versions of Kiota when updating the client.
Add the following to your composer.json to set your namespaces correctly:
To ensure the newly generated classes can be imported, update the autoload paths using:
Bash
composer dumpautoload
Create the client application
Create a file in the root of the project named main.php and add the following code.
PHP
<?phpuseKiotaPosts\Client\PostsApiClient;
useKiotaPosts\Client\Models\Post;
useMicrosoft\Kiota\Abstractions\ApiException;
useMicrosoft\Kiota\Abstractions\Authentication\AnonymousAuthenticationProvider;
useMicrosoft\Kiota\Http\GuzzleRequestAdapter;
require__DIR__.'/vendor/autoload.php';
try {
$authProvider = new AnonymousAuthenticationProvider();
$requestAdapter = new GuzzleRequestAdapter($authProvider);
$client = new PostsApiClient($requestAdapter);
// GET /posts
$allPosts = $client->posts()->get()->wait();
$postCount = sizeof($allPosts);
echo"Retrieved {$postCount} posts.\n";
// GET /posts/{id}
$specificPostId = 5;
$specificPost = $client->posts()->byPostId($specificPostId)->get()->wait();
echo"Retrieved post - ID: {$specificPost->getId()}, Title: {$specificPost->getTitle()}, Body: {$specificPost->getBody()}\n";
// POST /posts
$newPost = new Post();
$newPost->setUserId(42);
$newPost->setTitle("Testing Kiota-generated API client");
$newPost->setBody("Hello world!");
$createdPost = $client->posts()->post($newPost)->wait();
echo"Created new post with ID: {$createdPost->getId()}\n";
// PATCH /posts/{id}
$update = new Post();
// Only update title
$update->setTitle("Updated title");
$updatedPost = $client->posts()->byPostId($specificPostId)->patch($update)->wait();
echo"Updated post - ID: {$updatedPost->getId()}, Title: {$updatedPost->getTitle()}, Body: {$updatedPost->getBody()}\n";
// DELETE /posts/{id}
$client->posts()->byPostId($specificPostId)->delete()->wait();
}catch (ApiException $ex) {
echo $ex->getMessage();
}
?>
Hinweis
The JSONPlaceholder REST API doesn't require any authentication, so this sample uses the AnonymousAuthenticationProvider. For APIs that require authentication, use an applicable authentication provider.
Run the application
To start the application, run the following command in your project directory.
Nutzen eines REST-Webdiensts mithilfe von HttpClient und Ausführen grundlegender CRUD-Vorgänge. Sie werden erkennen, wann Ihr Gerät mit dem Internet verbunden ist, um eine gute Benutzererfahrung zu bieten, und Sie können die Vorteile der nativen Netztechnologiestapel nutzen, um Spitzenleistungen zu erzielen.