1. Go to this page and download the library: Download sevaske/php-zatca-xml 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/ */
sevaske / php-zatca-xml example snippets
use Saleh7\Zatca\CertificateBuilder;
use Saleh7\Zatca\Exceptions\CertificateBuilderException;
try {
(new CertificateBuilder())
->setOrganizationIdentifier('312345678901233') // The Organization Identifier must be 15 digits, starting andending with 3
// string $solutionName .. The solution provider name
// string $model .. The model of the unit the stamp is being generated for
// string $serialNumber .. # If you have multiple devices each should have a unique serial number
->setSerialNumber('Saleh', '1n', 'SME00023')
->setCommonName('My Organization') // The common name to be used in the certificate
->setCountryName('SA') // The Country name must be Two chars only
->setOrganizationName('My Company') // The name of your organization
->setOrganizationalUnitName('IT Department') // A subunit in your organizatio
->setAddress('Riyadh 1234 Street') // like Riyadh 1234 Street
->setInvoiceType(1100)// # Four digits, each digit acting as a bool. The order is as follows: Standard Invoice, Simplified, future use, future use
->setProduction(false)// true = Production | false = Testing
->setBusinessCategory('Technology') // Your business category like food, real estate, etc
->generateAndSave('output/certificate.csr', 'output/private.pem');
echo "Certificate and private key saved.\n";
} catch (CertificateBuilderException $e) {
echo "Error: " . $e->getMessage() . "\n";
exit(1);
}
use GuzzleHttp\Client;
use Sevaske\ZatcaApi\Api;
use Sevaske\ZatcaApi\Exceptions\ZatcaException;
$api = new Api('sandbox', new Client);
$certificatePath = __DIR__.'/output/certificate.csr';
$csr = file_get_contents($certificatePath);
try {
$response = $api->complianceCertificate($csr, '123123');
$credentials = [
'requestId' => $response->requestId(),
'certificate' => $response->certificate(),
'secret' => $response->secret(),
];
print_r($credentials);
// sava file output/ZATCA_certificate_data.json
$outputFile = __DIR__.'/output/ZATCA_certificate_data.json';
file_put_contents($outputFile, json_encode($credentials, JSON_PRETTY_PRINT));
echo "\nCertificate data saved to {$outputFile}\n";
} catch (ZatcaException $e) {
echo 'API Error: '.$e->getMessage()."\n";
print_r($e->context());
} catch (\Exception $e) {
echo 'Error: '.$e->getMessage();
}