microsoft/kiota-serialization-form
PHP application/x-www-form-urlencoded serialization library for Microsoft Kiota generated SDKs. Adds support for form-encoded request/response payloads from compatible API endpoints; install via Composer and use alongside Kiota PHP projects.
Installation:
composer require microsoft/kiota-serialization-form
Ensure your composer.json includes:
"require": {
"microsoft/kiota-serialization-form": "^2.0.2"
}
Kiota Integration:
application/x-www-form-urlencoded serialization for Kiota-generated clients.MyApiClient), configure it to use the form serializer:
use Microsoft\Kiota\Serialization\Form\FormSerializer;
$serializer = new FormSerializer();
$client = new MyApiClient($serializer);
First Use Case:
$requestInfo = new RequestInformation();
$requestInfo->setMethod("POST");
$requestInfo->setUrl("https://api.example.com/data");
$content = new FormContent();
$content->add("name", "John Doe");
$content->add("active", "true");
$serializer = new FormSerializer();
$serializedContent = $serializer->serialize($content);
// Outputs: "name=John+Doe&active=true"
Request Serialization:
FormSerializer to convert Kiota FormContent objects to application/x-www-form-urlencoded strings:
$serializer = new FormSerializer();
$serialized = $serializer->serialize($formContent);
RequestInformation:
$requestInfo->setBodyContent($serialized);
Response Deserialization:
$response = $client->request($requestInfo);
$deserializer = new FormDeserializer();
$parsedContent = $deserializer->deserialize($response->getBody(), new FormContent());
Complex Types:
FormContent for custom models:
class UserFormContent extends FormContent {
public function addUserData(User $user) {
$this->add("user[name]", $user->getName());
$this->add("user[email]", $user->getEmail());
}
}
Laravel HTTP Clients:
Integrate with Laravel’s Http facade by extending ClientRequest:
use Microsoft\Kiota\Serialization\Form\FormSerializer;
use Illuminate\Http\Client\Request;
class KiotaRequest extends Request {
public function __construct(FormSerializer $serializer) {
$this->serializer = $serializer;
}
public function serialize(): string {
return $this->serializer->serialize($this->content);
}
}
Middleware: Use middleware to auto-attach form serialization:
public function handle($request, Closure $next) {
if ($request->hasHeader('Content-Type', 'application/x-www-form-urlencoded')) {
$request->merge([
'serializer' => new FormSerializer()
]);
}
return $next($request);
}
Testing:
Mock FormSerializer in unit tests:
$mockSerializer = $this->createMock(FormSerializer::class);
$mockSerializer->method('serialize')->willReturn("name=Test&active=true");
$client = new MyApiClient($mockSerializer);
Boolean Handling:
"false" strings to false (fixed in v2.0.1), but ensure your API expects lowercase "true"/"false" for consistency.$content->add("is_active", "false"); // Deserializes to `false`
PHP Version:
URL Encoding:
+ (per application/x-www-form-urlencoded spec). Avoid manual urlencode() calls unless overriding FormSerializer.Nested Objects:
user[address][city]). Implement custom FormContent classes for complex structures.Empty Values:
"") or null values are omitted from the serialized output. Explicitly handle missing fields in your API logic.Validation Errors: If deserialization fails, check for:
& or =).FormDeserializer::getParseNode() to inspect raw parsed values:
$parseNode = $deserializer->getParseNode($responseBody);
dump($parseNode->getValue()); // Debug raw parsed data
Performance:
FormContent::addMultiple() to reduce allocations (optimized in v2.0.1+).Custom Serialization:
Override FormSerializer to modify behavior:
class CustomFormSerializer extends FormSerializer {
public function serialize(FormContent $content): string {
$data = [];
foreach ($content->getProperties() as $key => $value) {
$data[$key] = strtoupper($value); // Example: Force uppercase
}
return http_build_query($data);
}
}
Type Casting:
Extend FormDeserializer to handle custom types:
$deserializer = new FormDeserializer();
$deserializer->setTypeMapper([
'date' => function ($value) {
return \Carbon\Carbon::parse($value);
}
]);
Kiota Model Integration:
Use traits to auto-generate FormContent for Eloquent models:
trait FormContentable {
public function toFormContent(): FormContent {
$content = new FormContent();
foreach ($this->getFillable() as $field) {
$content->add($field, $this->{$field});
}
return $content;
}
}
How can I help you explore Laravel packages today?