Skip to content

Conversation

@oguzhankrcb
Copy link
Contributor

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 code and jobID values from XML
    • Normalizes single-digit codes (0 → 00, 1 → 01, 2 → 02)
    • Error checking and exception handling
  • 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:

  1. it_can_parse_xml_response_successfully: Verifies that XML format is parsed successfully
  2. it_can_parse_xml_response_with_single_digit_code: Tests that single-digit codes (0, 1, 2) are normalized correctly
  3. it_can_parse_legacy_space_separated_response: Verifies that the old format still works (backward compatibility)
  4. it_throws_exception_for_invalid_xml_response_code: Tests that an exception is thrown for invalid codes
  5. 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-simplexml extension for XML parsing (already a requirement in composer.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

  • Code changes implemented
  • Unit tests added
  • Backward compatibility maintained
  • Documentation updated (PR description)
  • All tests passing

… XML-formatted responses while maintaining legacy response handling for API compatibility.
@hozdemir
Copy link
Member

hozdemir commented Nov 4, 2025

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 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants