如何用PHP和Curl搭建接收API请求并返回7101 XML响应的服务
How to Build a PHP Receiver for XML API Requests & Return Specified Response
Hey there, I totally get how stuck you might feel after a week working on this receiver—let’s break this down step by step and get your cross-system data flow working smoothly.
First, let’s clarify what your receiver needs to do:
- Accept incoming POST requests with XML payloads (matching what your sender script sends)
- (Optional but recommended) Validate incoming requests to ensure they’re legitimate
- Return the exact
<gepgBillSubReqAck>XML success response you specified
Here’s a complete, tested PHP receiver code:
<?php // 1. Read the raw XML payload from the incoming POST request $incomingXml = file_get_contents('php://input'); // Optional: Log incoming requests for debugging (super helpful if things go wrong) file_put_contents('incoming_requests.log', date('Y-m-d H:i:s') . "\n" . $incomingXml . "\n\n", FILE_APPEND); // 2. Optional: Validate request headers (matches the headers your sender uses) // Check if required headers exist (Gepg-Com and Gepg-Code from your sender script) $requiredHeaders = ['Gepg-Com', 'Gepg-Code']; $missingHeaders = []; foreach ($requiredHeaders as $header) { $serverHeaderKey = 'HTTP_' . strtoupper(str_replace('-', '_', $header)); if (!isset($_SERVER[$serverHeaderKey])) { $missingHeaders[] = $header; } } if (!empty($missingHeaders)) { // Return error response if headers are missing (adjust as needed) http_response_code(400); echo "Missing required headers: " . implode(', ', $missingHeaders); exit; } // 3. Prepare your predefined success response XML $responseXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <gepgBillSubReqAck> <TrxStsCode>7101</TrxStsCode> </gepgBillSubReqAck>'; // 4. Set proper HTTP headers to signal an XML response header('Content-Type: application/xml; charset=utf-8'); header('Content-Length: ' . strlen($responseXml)); // 5. Send the response back to the sender echo $responseXml; exit; ?>
Key Details to Keep in Mind:
- Reading the Request:
file_get_contents('php://input')is the standard way to capture raw POST data (like XML) in PHP—it works perfectly for this use case, regardless of the content type. - Logging: The
incoming_requests.logfile will let you confirm if the receiver is actually getting data from your sender. Just make sure your web server has write permissions for the directory where this script lives. - Header Validation: I added checks for the headers your sender uses (
Gepg-ComandGepg-Code)—this is optional but adds a basic security layer to ensure only valid requests are processed. You can skip this if you don’t need it, but it’s a good practice. - Response Headers: Setting
Content-Type: application/xmltells the sender that we’re returning XML, which matches what your sender expects. IncludingContent-Lengthhelps the sender properly handle the response.
Testing the Receiver:
- Upload this script to your target server (name it something like
bill_receiver.php) - Update your sender script’s
$serverIpand$urito point to this new script (e.g.,$serverIp = "https://your-receiver-server.com"; $uri = "/bill_receiver.php";) - Run your sender script—you should see the
<gepgBillSubReqAck>success response printed out.
Troubleshooting Tips (If It Still Doesn’t Work):
- Check Server Logs: Look at your web server’s error logs (Apache’s
error.logor Nginx’serror.log) for any PHP errors that might be breaking the script. - Test Directly with Curl: Send a test request to your receiver to confirm it’s reachable:
You should get your success XML response back immediately.curl -X POST -H "Content-Type: application/xml" -H "Gepg-Com:default.sp.in" -H "Gepg-Code:SP103" -d "<test>Sample Request</test>" https://your-receiver-server.com/bill_receiver.php - Permissions: Make sure the receiver script has execute permissions, and the log file (if used) can be written to by the web server user.
That’s it—this should get your cross-system data interaction up and running. Let me know if you hit any snags along the way!
内容的提问来源于stack exchange,提问作者konz laugoko
相关产品推荐
相关产品推荐

