Exemplu PHP 8.3 cu SOAP
Acest POC arată o integrare PHP 8.3 folosind extensia nativă ext-soap și SoapClient.
Exemplele folosesc date fictive. Pentru publicare reală, completează câmpurile recomandate din Validare și câmpuri recomandate.
Cerințe
Verifică dacă extensia SOAP este activă:
php -m | grep soap
WSDL-ul folosit în exemple:
https://www.imopedia.ro/soap.wsdl
Client minimal
<?php
declare(strict_types=1);
$wsdl = 'https://www.imopedia.ro/soap.wsdl';
$client = new SoapClient($wsdl, [
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => true,
'exceptions' => true,
]);
$sessionId = $client->__soapCall('login', [
'username' => 'user@example.com',
'password' => 'parola',
]);
try {
$property = [
'AGENTIA' => 123,
'ID_LOCAL' => 67654,
'AGENT_ID' => 45,
'IMOPEDIA' => 1,
'TIP_IMOBIL_REAL' => 1,
'DATA_APARITIE' => '2026-05-28',
'DATA_MODIFICARE' => '2026-05-28',
'TITLU' => 'Apartament 2 camere - exemplu',
'T_V_TRANZ' => 1,
'T_V_PRET' => 120000,
'T_V_MONEDA' => 'EUR',
'JUDET' => 'Bucuresti',
'JUDET_ID' => 10,
'ORAS' => 'Bucuresti',
'ZONA' => 'Central',
'CONTACT_PERS' => 'Agent Exemplu',
'CONTACT_EMAIL' => 'agent@example.com',
'CONTACT_TEL' => '+40 700 000 000',
];
$client->__soapCall('saveAgent', [
'session_id' => $sessionId,
'options' => arrayToObject([
'AGENTIA' => 123,
'AGENT_ID' => 45,
'CONTACT_PERS' => 'Agent Exemplu',
'CONTACT_EMAIL' => 'agent@example.com',
'CONTACT_TEL' => '+40 700 000 000',
]),
]);
$result = $client->__soapCall('saveProperty', [
'session_id' => $sessionId,
'options' => arrayToObject($property),
]);
var_dump($result);
} finally {
$client->__soapCall('logout', [
'session_id' => $sessionId,
]);
}
function arrayToObject(array $array): stdClass
{
$object = new stdClass();
foreach ($array as $name => $value) {
$object->{$name} = is_array($value) ? arrayToObject($value) : $value;
}
return $object;
}
Wrapper simplu cu SoapClient
Pentru o integrare reală, este mai practic să izolezi autentificarea, execuția operațiilor și închiderea sesiunii într-o clasă mică.
<?php
declare(strict_types=1);
final class ImopediaSoapClient
{
private SoapClient $client;
private ?string $sessionId = null;
public function __construct(private string $wsdl)
{
$this->client = new SoapClient($this->wsdl, [
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => true,
'exceptions' => true,
]);
}
public function login(string $username, string $password): void
{
$this->sessionId = (string) $this->client->__soapCall('login', [
'username' => $username,
'password' => $password,
]);
}
public function saveProperty(array $property): mixed
{
return $this->call('saveProperty', [
'options' => $this->arrayToObject($property),
]);
}
public function logout(): void
{
if ($this->sessionId === null) {
return;
}
$this->client->__soapCall('logout', [
'session_id' => $this->sessionId,
]);
$this->sessionId = null;
}
private function call(string $method, array $params): mixed
{
if ($this->sessionId === null) {
throw new RuntimeException('Clientul nu este autentificat.');
}
return $this->client->__soapCall($method, [
'session_id' => $this->sessionId,
...$params,
]);
}
private function arrayToObject(array $array): stdClass
{
$object = new stdClass();
foreach ($array as $name => $value) {
$object->{$name} = is_array($value) ? $this->arrayToObject($value) : $value;
}
return $object;
}
}
$wsdl = 'https://www.imopedia.ro/soap.wsdl';
$api = new ImopediaSoapClient($wsdl);
$api->login('user@example.com', 'parola');
try {
$response = $api->saveProperty([
'AGENTIA' => 123,
'ID_LOCAL' => 67654,
'AGENT_ID' => 45,
'IMOPEDIA' => 1,
'TIP_IMOBIL_REAL' => 1,
'DATA_APARITIE' => '2026-05-28',
'DATA_MODIFICARE' => '2026-05-28',
'TITLU' => 'Apartament 2 camere - exemplu',
'T_V_TRANZ' => 1,
'T_V_PRET' => 120000,
'T_V_MONEDA' => 'EUR',
'JUDET' => 'Bucuresti',
'JUDET_ID' => 10,
'ORAS' => 'Bucuresti',
'ZONA' => 'Central',
'CONTACT_PERS' => 'Agent Exemplu',
'CONTACT_EMAIL' => 'agent@example.com',
'CONTACT_TEL' => '+40 700 000 000',
]);
var_dump($response);
} finally {
$api->logout();
}
Pentru fluxul complet cu media, vezi Exemple media.