PHP code example of schababerle-digital / odata-client
1. Go to this page and download the library: Download schababerle-digital/odata-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
schababerle-digital / odata-client example snippets
chababerleDigital\OData\Client\V4\Client as ODataV4Client;
use SchababerleDigital\OData\Http\GuzzleHttpClient; // Beispiel-HTTP-Client
use SchababerleDigital\OData\Client\V4\ResponseParser as ODataV4ResponseParser; // Beispiel-Parser
use SchababerleDigital\OData\Serializer\JsonSerializer; // Beispiel-Serializer
use SchababerleDigital\OData\QueryBuilder\FilterBuilder;
use SchababerleDigital\OData\Contract\QueryBuilderInterface;
// 1. HTTP-Client initialisieren (Guzzle als Beispiel)
$guzzleClient = new \GuzzleHttp\Client([
// 'auth' => ['username', 'password'], // Optional: Authentifizierung
// 'headers' => ['X-Custom-Header' => 'Value'] // Optionale Standard-Header
]);
$httpClient = new GuzzleHttpClient($guzzleClient);
// 2. Parser und Serializer initialisieren
$responseParser = new ODataV4ResponseParser();
$jsonSerializer = new JsonSerializer();
// 3. OData-Client instanziieren
$odataServiceUrl = '[https://services.odata.org/V4/TripPinServiceRW/](https://services.odata.org/V4/TripPinServiceRW/)'; // Beispiel-Dienst
$client = new ODataV4Client($httpClient, $responseParser, $jsonSerializer, $odataServiceUrl);
try {
// Beispiel: Alle Personen abrufen und nur Name und E-Mails auswählen
echo "Fetching all people...\n";
$people = $client->find('People', function(QueryBuilderInterface $qb) {
$qb->select(['UserName', 'FirstName', 'LastName', 'Emails'])
->top(5);
});
foreach ($people as $person) {
echo "User: " . $person->getProperty('UserName') . " - Name: " . $person->getProperty('FirstName') . " " . $person->getProperty('LastName') . "\n";
if (is_array($person->getProperty('Emails'))) {
foreach ($person->getProperty('Emails') as $email) {
echo " Email: " . $email . "\n";
}
}
}
echo "Next Link for People: " . ($people->getNextLink() ?? 'None') . "\n\n";
// Beispiel: Eine bestimmte Person abrufen
$userNameToFetch = 'russellwhyte';
echo "Fetching person: " . $userNameToFetch . "...\n";
$person = $client->get('People', $userNameToFetch, function(QueryBuilderInterface $qb) {
$qb->select(['FirstName', 'LastName', 'Gender']);
});
echo "Details for " . $person->getProperty('FirstName') . ":\n";
print_r($person->getProperties());
echo "\n";
// Beispiel: FilterBuilder verwenden
echo "Fetching people filtered by name...\n";
$filter = FilterBuilder::new()
->where('FirstName')->startsWith('Russell')
->and()
->where('LastName')->equals('Whyte')
->build();
$filteredPeople = $client->find('People', function(QueryBuilderInterface $qb) use ($filter) {
$qb->filter($filter)->select(['UserName', 'FirstName', 'LastName']);
});
foreach ($filteredPeople as $filteredPerson) {
echo "Filtered User: " . $filteredPerson->getProperty('UserName') . "\n";
}
// Weitere Beispiele (CRUD, Funktionen, Actions) könnten hier folgen...
} catch (\SchababerleDigital\OData\Exception\ODataException $e) {
echo "OData Error: " . $e->getMessage() . "\n";
if ($e instanceof \SchababerleDigital\OData\Exception\HttpResponseException && $e->getResponse()) {
echo "Status Code: " . $e->getResponse()->getStatusCode() . "\n";
// echo "Response Body: " . (string) $e->getResponse()->getBody() . "\n";
}
// Debugging: $e->getODataError() kann detailliertere Fehler vom Dienst enthalten, falls geparst
} catch (\Exception $e) {
echo "General Error: " . $e->getMessage() . "\n";
}
use SchababerleDigital\OData\Client\V2\Client as ODataV2Client;
use SchababerleDigital\OData\Client\V2\ResponseParser as ODataV2ResponseParser;
// ...
$client = new ODataV2Client($httpClient, new ODataV2ResponseParser(), $jsonSerializer, $v2ServiceUrl);
// ...
try {
$newPersonData = [
'UserName' => 'newuser' . rand(1000, 9999),
'FirstName' => 'Test',
'LastName' => 'User',
'Emails' => ['[email protected]'],
'AddressInfo' => [ // Komplexer Typ
[
'Address' => '123 Main St',
'City' => [
'Name' => 'MyCity',
'CountryRegion' => 'US',
'Region' => 'CA'
]
]
],
// Um eine Verknüpfung zu einer existierenden Entität herzustellen (z.B. zu einem Trip):
// '[email protected]' => [ "Trips(0)" ] // Verknüpft diesen neuen User mit Trip mit ID 0
];
// $newEntity = new \SchababerleDigital\OData\Client\Common\Entity('Person', $newPersonData);
// $createdPerson = $client->create('People', $newEntity);
// Besser: direkt Array übergeben, Client wandelt es um oder man erstellt eine spezifische Entitätsklasse.
$createdPerson = $client->create('People', $newPersonData);
echo "Created Person UserName: " . $createdPerson->getProperty('UserName') . " with ID: " . $createdPerson->getId() . "\n";
} catch (\SchababerleDigital\OData\Exception\ODataException $e) {
// Fehlerbehandlung
echo "Error creating person: " . $e->getMessage() . "\n";
if ($e instanceof \SchababerleDigital\OData\Exception\HttpResponseException && $e->getResponse()) {
echo "Raw Response: " . (string) $e->getResponse()->getBody() . "\n";
}
}
$guzzleClient = new \GuzzleHttp\Client([
'timeout' => 10.0, // Timeout in Sekunden
'auth' => ['username', 'password', 'digest'], // Authentifizierung
'proxy' => 'http://localhost:8123', // Proxy
'headers' => [
'X-Custom-Global-Header' => 'MeineAnwendung'
]
]);
$httpClient = new GuzzleHttpClient($guzzleClient);
// ... Client mit diesem $httpClient initialisieren
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.