{"id":16257,"date":"2023-11-09T07:55:37","date_gmt":"2023-11-09T15:55:37","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/?p=16257"},"modified":"2023-11-09T07:55:37","modified_gmt":"2023-11-09T15:55:37","slug":"write-high-quality-code-with-the-microsoft-graph-php-sdk-v2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/write-high-quality-code-with-the-microsoft-graph-php-sdk-v2\/","title":{"rendered":"Write high quality code with the Microsoft Graph PHP SDK v2"},"content":{"rendered":"<p>In July 2022, we <a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/microsoft-graph-php-sdk-2-0-0-rc5-is-now-available\/\">announced<\/a> the public preview of the first Kiota generated SDK for PHP. Today, we are pleased to make this SDK generally available. The Microsoft Graph PHP SDK 2.0.0 offers best in class features to improve developer efficiency and code quality. By solving cross-cutting concerns like authentication, retry and batching, the SDK gives you time back to focus on the design and value of your application. Let\u2019s explore some of its most important features.<\/p>\n<h2>Authentication provider<\/h2>\n<p>The authentication provider wraps around PHP League\u2019s\u00a0<a href=\"https:\/\/oauth2-client.thephpleague.com\/\" target=\"_blank\" rel=\"noopener\">OAuth 2.0 client<\/a>, hence, its name \u2013 `GraphPhpLeagueAuthenticationProvider`.<\/p>\n<p>The authentication provider supports the following flows:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/v2-oauth2-auth-code-flow\" target=\"_blank\" rel=\"noopener\">OAuth 2.0 auth code grant<\/a>\u00a0for delegated permissions.<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/v2-oauth2-client-creds-grant-flow\" target=\"_blank\" rel=\"noopener\">OAuth 2.0 client credentials grant<\/a>\u00a0for application permissions.<\/li>\n<\/ul>\n<p>It also supports\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/v2-oauth2-on-behalf-of-flow\">OAuth 2.0 on-behalf-of flow<\/a>\u00a0to request tokens on behalf of third-party applications.<\/p>\n<p>The auth provider guarantees the token is always valid by tracking the expiration and automatically refreshing it in the background before it expires.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/isvargasmsft\/65e98224ed196e77ff5704b0262f35cf.js\"><\/script><\/p>\n<h2>Fluent experience to build requests<\/h2>\n<p>Aided by the IDE\u2019s autocomplete, the SDK helps you find the right command in each line of code, improving discoverability and reducing the time spent reading documentation. The SDK suggests only commands that work with the last typed instruction, reducing errors in your code.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16258\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2023\/11\/screenshot-showing-an-example-of-the-fluent-experi.png\" alt=\"Screenshot showing Microsoft Graph PHP SDK v2 in use\" width=\"856\" height=\"150\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2023\/11\/screenshot-showing-an-example-of-the-fluent-experi.png 856w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2023\/11\/screenshot-showing-an-example-of-the-fluent-experi-300x53.png 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2023\/11\/screenshot-showing-an-example-of-the-fluent-experi-768x135.png 768w\" sizes=\"(max-width: 856px) 100vw, 856px\" \/><\/p>\n<p><script src=\"https:\/\/gist.github.com\/isvargasmsft\/616952b427cc77456b76215640869095.js\"><\/script><\/p>\n<h2>Built-in retry handler<\/h2>\n<p>The SDK offers a configurable built-in Retry-Handler that understands 429 (Too Many Requests), 503 (Service Unavailable) and 504 (Gateway Timeout) response status codes, that usually require the application to retry the request after some time. The handler reads the recommendation in the `Retry-After` header and retries after the recommended time automatically. You can also customize the handler and specify a\u00a0custom value for the\u00a0`RetriesTimeLimit`\u00a0greater than 0, to introduce time-based evaluated request retries with the default count-based request retry.<\/p>\n<h2>Page iterator<\/h2>\n<p>For performance reasons, collections of entities are often split into pages and each page is returned with a URL to the next page. The PageIterator class simplifies consuming of paged collections. PageIterator handles enumerating the current page and requesting subsequent pages automatically.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/isvargasmsft\/c66a4c76b01ce25400c6bdf63268882c.js\"><\/script><\/p>\n<h2>Batching<\/h2>\n<p>Combine multiple requests in a single call with ease. Up to 20 individual requests can be batched together to reduce network latency of making each request separately. The BatchRequestBuilder allows you to make requests to the \/$batch endpoint of the Microsoft Graph API.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/isvargasmsft\/ebed5a186b5527940335d2b60aa1dd37.js\"><\/script><\/p>\n<h2>Uploading large files<\/h2>\n<p>To upload files larger than 3MB, Microsoft Graph API supports uploads using resumable upload sessions where several bytes are uploaded at a time. The SDK provides a LargeFileUpload task that slices your file into bytes and progressively uploads them until completion.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/isvargasmsft\/c7a165be9a0582fe34fe2c58f792cff8.js\"><\/script><\/p>\n<h2>Try out the new version today!<\/h2>\n<p>Get started now with the <a href=\"https:\/\/github.com\/microsoftgraph\/msgraph-sdk-php\/blob\/dev\/UPGRADING.md\">Upgrade guide<\/a>, <a href=\"https:\/\/github.com\/microsoftgraph\/msgraph-sdk-php\/blob\/dev\/README.md\">the README<\/a> and <a href=\"https:\/\/github.com\/microsoftgraph\/msgraph-sdk-php\/blob\/dev\/docs\/Examples.md\">code samples.<\/a> We have written a detailed description of the breaking changes introduced by this new version. You can also find the packages for the beta endpoint here.<\/p>\n<p>Please don\u2019t hesitate to <a href=\"https:\/\/github.com\/microsoftgraph\/msgraph-sdk-php\/issues\/new\">open an issue<\/a> in <a href=\"https:\/\/github.com\/microsoftgraph\/msgraph-sdk-php\">our repository<\/a> for any errors or breaking changes you encounter that we didn\u2019t include in our guide.<\/p>\n<h2>Learn more about Microsoft Graph SDKs<\/h2>\n<p>The SDK includes a set of tools, libraries, and documentation that make it easy to get started building applications that take advantage of the rich data and insights available through Microsoft Graph API.<\/p>\n<p>Start saving time and removing complexity in your code today.\u00a0Sign up for the\u00a0<a href=\"https:\/\/developer.microsoft.com\/en-us\/microsoft-365\/dev-program\" target=\"_blank\" rel=\"noopener\">Microsoft 365 Developer Program<\/a>\u00a0to get a free Microsoft 365 developer. We also recommend that you join our\u00a0<a href=\"https:\/\/aka.ms\/m365\/community\" target=\"_blank\" rel=\"noopener\">Microsoft 365 Platform community<\/a>\u00a0calls and events and get involved with other developers who use Microsoft Graph and build solutions for Microsoft 365.<\/p>\n<p>Here are some additional resources we have pulled together to help you get started.<\/p>\n<ul>\n<li>Explore\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/graph\/sdks\/sdks-overview\" target=\"_blank\" rel=\"noopener\">our public documentation<\/a>, where you can find how to install the SDK, authenticate, customize your client and more.<\/li>\n<li>Use the\u00a0<a href=\"https:\/\/aka.ms\/ge\" target=\"_blank\" rel=\"noopener\">Microsoft Graph Explorer<\/a>, a tool that lets you make requests and see responses against the Microsoft Graph, and which displays corresponding snippets to requests you make.<\/li>\n<li>Visit the\u00a0<a href=\"https:\/\/developer.microsoft.com\/graph\" target=\"_blank\" rel=\"noopener\">Microsoft Graph Dev Center<\/a>.<\/li>\n<li>Follow us on <a href=\"http:\/\/twitter.com\/microsoft365dev\">X (Twitter) \/ @Microsoft365Dev<\/a> for the latest news and announcements.<\/li>\n<\/ul>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By solving cross-cutting concerns like authentication, retry, and batching, PHP SDK v2 gives you time back to focus on the design and value of your application. <\/p>\n","protected":false},"author":95132,"featured_media":16260,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[180],"class_list":["post-16257","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-microsoft-graph-php-sdk"],"acf":[],"blog_post_summary":"<p>By solving cross-cutting concerns like authentication, retry, and batching, PHP SDK v2 gives you time back to focus on the design and value of your application. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/16257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/95132"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=16257"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/16257\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/16260"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=16257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=16257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=16257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}