Fix otp xml message response parsing
Add XML Response Format Support
Summary
Due to recent changes in the Netgsm API response format, the package now supports both the new XML format and the legacy space-separated format. This change is backward compatible and does not affect existing applications.
Problem
The Netgsm API recently changed its response format and now returns responses in XML format like this:
<?xml version="1.0"?>
<xml><main><code>0</code><jobID>176217829127282816710591819</jobID></main></xml>
The previously used format was:
00 123456789
Since the existing code only supported the space-separated format, responses in the new XML format could not be parsed correctly.
Solution
1. Updated Response Parsing Mechanism (src/Sms/AbstractNetgsmMessage.php)
Changes:
-
Refactored
parseResponse()method: Now automatically detects the response format and calls the appropriate parser method -
Added
parseXmlResponse()method: Parses responses in XML format- Parses XML using
simplexml_load_string() - Extracts
codeandjobIDvalues from XML - Normalizes single-digit codes (0 → 00, 1 → 01, 2 → 02)
- Error checking and exception handling
- Parses XML using
-
Added
parseLegacyResponse()method: Parses the old space-separated format- Preserves existing logic
- Ensures backward compatibility
Code Flow:
parseResponse()
├─ Is XML? → parseXmlResponse()
└─ Otherwise → parseLegacyResponse()
2. Added Comprehensive Test Coverage (tests/NetGsmMessageTest.php)
New Tests:
- ✅
it_can_parse_xml_response_successfully: Verifies that XML format is parsed successfully - ✅
it_can_parse_xml_response_with_single_digit_code: Tests that single-digit codes (0, 1, 2) are normalized correctly - ✅
it_can_parse_legacy_space_separated_response: Verifies that the old format still works (backward compatibility) - ✅
it_throws_exception_for_invalid_xml_response_code: Tests that an exception is thrown for invalid codes - ✅
it_throws_exception_for_missing_job_id_in_xml: Tests that an exception is thrown for empty jobID
Test Refactoring:
-
Added helper method:
setResponseAndParse()- Moved repetitive reflection code into a common method - Reduced code duplication and improved test readability
Backward Compatibility
✅ 100% Backward Compatible: Support for the old space-separated format is preserved. Existing applications will continue to work without any changes.
Tested Scenarios
| Scenario | Format | Status |
|---|---|---|
| Successful XML response | XML | ✅ Pass |
| Single-digit code (0, 1, 2) | XML | ✅ Pass |
| Successful legacy response | Space-separated | ✅ Pass |
| Invalid code | XML | ✅ Throws exception |
| Empty jobID | XML | ✅ Throws exception |
| Invalid code | Legacy | ✅ Throws exception |
Example Usage
No changes required in user code:
use TarfinLabs\Netgsm\Netgsm;
use TarfinLabs\Netgsm\Sms\NetgsmSmsMessage;
// Code continues to work without changes
$message = new NetgsmSmsMessage("Hello!");
$message->setRecipients('5051234567');
$jobId = Netgsm::sendSms($message); // ✅ Both XML and legacy formats supported
Changed Files
-
src/Sms/AbstractNetgsmMessage.php: Updated response parsing mechanism -
tests/NetGsmMessageTest.php: Added 5 new tests + helper method
Notes
- Uses
ext-simplexmlextension for XML parsing (already a requirement incomposer.json) - Thanks to code normalization, works seamlessly even if the API returns variable formats like
0,00,1,01,2,02 - All existing tests pass successfully
Checklist
- [x] Code changes implemented
- [x] Unit tests added
- [x] Backward compatibility maintained
- [x] Documentation updated (PR description)
- [x] All tests passing
Eline sağlık Oğuzhan, gerçekten güzel toparlamışsın 🙌 Bu arada Netgsm artık JSON response da destekliyor. Belki legacy ve XML parse işlemlerini tamamen bırakıp doğrudan JSON’a geçmek daha mantıklı olabilir. Kod oldukça sadeleşir ve bakım tarafı da bizim için çok daha kolay hale gelir. Eğer istersen, bu yönde bir PR’ı memnuniyetle değerlendiririz 😊
Eline sağlık Oğuzhan, gerçekten güzel toparlamışsın 🙌 Bu arada Netgsm artık JSON response da destekliyor. Belki legacy ve XML parse işlemlerini tamamen bırakıp doğrudan JSON’a geçmek daha mantıklı olabilir. Kod oldukça sadeleşir ve bakım tarafı da bizim için çok daha kolay hale gelir. Eğer istersen, bu yönde bir PR’ı memnuniyetle değerlendiririz 😊
Abim selamlar, çok teşekkürler, şu anda kendi projemde kullanıyorum paketi ve OTP endpointinin JSON implementation'u henüz yapılmamış normal SMS gönderimleri için JSON desteği var sadece, OTP için JSON desteği eklediklerinde bu geliştirmeyi de seve seve yapabilirim, şimdilik bunu bu şekilde merge edebilirsek çok iyi olur, herkese çok selamlar, saygılar.
Eline sağlık Oğuzhan, gerçekten güzel toparlamışsın 🙌 Bu arada Netgsm artık JSON response da destekliyor. Belki legacy ve XML parse işlemlerini tamamen bırakıp doğrudan JSON’a geçmek daha mantıklı olabilir. Kod oldukça sadeleşir ve bakım tarafı da bizim için çok daha kolay hale gelir. Eğer istersen, bu yönde bir PR’ı memnuniyetle değerlendiririz 😊
Abim selamlar, çok teşekkürler, şu anda kendi projemde kullanıyorum paketi ve OTP endpointinin JSON implementation'u henüz yapılmamış normal SMS gönderimleri için JSON desteği var sadece, OTP için JSON desteği eklediklerinde bu geliştirmeyi de seve seve yapabilirim, şimdilik bunu bu şekilde merge edebilirsek çok iyi olur, herkese çok selamlar, saygılar.
Çok teşekkürler Oğuzhan
Yoğunluktan dolayı geç yanıtladım kusura bakma. OTP tarafında JSON implementation geldiğinde yapıyı birlikte sadeleştiririz; şimdilik bu haliyle merge ediyorum. Eline sağlık.