如何将SOAP代码转换为PHP调用以集成货运追踪Web服务?
How to Convert Your SOAP Request to Working PHP Code
Hey Carlos, I totally get the frustration of wading through PHP's SOAP docs when you're just trying to get a service integrated into your site. Let's turn that partial code snippet into a complete, functional script that calls the RL Carriers Shipment Tracing Service.
Complete PHP Code Example
<?php // Define the WSDL endpoint $wsdl = "http://api.rlcarriers.com/1.0.2/ShipmentTracingService.asmx?wsdl"; // Your API key (replace with your actual key) $apiKey = 'xxxxxxxxxxxxxxxxxxxxxx'; // The PRO number(s) you want to trace - can be a single string or array $pro = '123456789'; // or pass an array like ['123456789', '987654321'] for multiple numbers // Build the full request parameters (matching the WSDL's expected structure) $request = [ 'APIKey' => $apiKey, 'traceNumbers' => $pro, 'TraceType' => 'PRO', 'FormatResults' => 'false', 'IncludeBlind' => 'false' // Adjust to 'true' if you need blind shipment data ]; try { // Initialize the SOAP client with debugging-friendly settings $client = new SoapClient($wsdl, [ 'trace' => true, // Enable request/response tracing for debugging 'exceptions' => true, // Throw exceptions on SOAP errors (easier to debug) 'cache_wsdl' => WSDL_CACHE_NONE // Disable WSDL caching during development ]); // Call the SOAP method - verify the exact method name via WSDL (likely "TraceShipments") $response = $client->__soapCall('TraceShipments', [$request]); // Process and display the response echo "<h3>SOAP Response:</h3>"; print_r($response); // Optional: Debug with raw request/response to match SoapUI echo "<h3>Raw Request Sent:</h3>"; echo htmlspecialchars($client->__getLastRequest()); echo "<h3>Raw Response Received:</h3>"; echo htmlspecialchars($client->__getLastResponse()); } catch (SoapFault $e) { // Handle SOAP errors gracefully echo "<h3>SOAP Error Occurred:</h3>"; echo "Error Code: " . $e->faultcode . "<br>"; echo "Error Message: " . $e->faultstring . "<br>"; // Optional: View the faulty request to spot mismatches echo "<h3>Faulty Request:</h3>"; echo htmlspecialchars($client->__getLastRequest()); } ?>
Key Details to Note
- SoapClient Setup: The extra options I added make debugging way easier—
tracelets you see exactly what's being sent/received, andexceptionsensures you get clear error messages instead of silent failures. Disabling WSDL caching prevents stale schema issues during development. - Request Parameters: I filled in the missing
IncludeBlindfield (adjust its value based on your needs). Double-check thattraceNumbersmatches the WSDL's expected type—if it accepts multiple PRO numbers, pass an array instead of a single string. - Method Name: Use
print_r($client->__getFunctions())to list all available methods from the WSDL if you're unsure of the exact name (it's almost certainly something likeTraceShipments).
Debugging Hack
Compare the raw PHP request (__getLastRequest()) with the working request you have in SoapUI. This is the fastest way to spot mismatches in parameter names, data types, or formatting that might be breaking the call.
内容的提问来源于stack exchange,提问作者Carlos
相关产品推荐
相关产品推荐

