PHP调用API执行SQL查询遇XML解析错误,寻求排查方案
我在PHP文件中通过API请求数据库,目前未得到有效返回结果,已添加多种调试逻辑。返回结果如下:
200 Error parsing XML
200表示连接成功,但存在XML解析错误。echo $response无内容输出(本该显示在200和Error parsing XML之间),我推测是$URL存在问题。请问是否有明显问题?或者还有其他调试方法?
我曾将$url .= '&SQLquery=' . urlencode($query);剪切到顶部,并从原始$URL变量中移除SQLQuery,但反而引发更多错误。这么做是因为看起来存在重复参数。
API文档示例:
http://localhost:82/sqlquery?apikey=ABC123&format=json&configuration=0
注:实际API密钥已替换为ABC123
我的PHP代码
<?php // API endpoint URL $url = 'http://orcus.companyname.local:82/SQLquery'; // API parameters $query = "Select RESOURCENAME from RESOURCEMASTER where RESOURCESTATUS='Active'"; $apiKey = 'ABC123'; $format = 'xml'; $configuration = 0; // Build the API URL with query parameters $url .= '?apikey=' . urlencode($apiKey); $url .= '&format=' . urlencode($format); $url .= '&configuration=' . urlencode($configuration); $url .= '&SQLquery=' . urlencode($query); // Create a new cURL resource $curl = curl_init(); // Set the cURL options curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //Enable verbose mode for cURL curl_setopt($curl, CURLOPT_VERBOSE, true); // Make the GET request and get the response $response = curl_exec($curl); // Get the HTTP response code $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); // Check if the request was successful if ($httpCode >= 200 && $httpCode < 300) { // Process the response // ... echo $httpCode. "<br>"; } else { echo "HTTP Error: " . $httpCode; } // Show structure of XML echo $response; // Check if an error occurred if ($response === false) { $error = curl_error($curl); echo "cURL Error: " . $error; } else { // Process the response // Assuming the response is in XML format $xml = simplexml_load_string($response); //Inspect structure of XML //var_dump($xml); // or print_r($xml); // Check parsing if ($xml === false) { echo "Error parsing XML\n"; exit; } // Debugging if(!$xml){ echo 'Fail to load XML file Simon.'; } // Handle the XML data as per your requirement // Example: Extracting the resource names $resourceNames = $xml->xpath('//RESOURCENAME'); // Output the resource names foreach ($resourceNames as $name) { echo $name . "<br>"; } } // Close the cURL resource curl_close($curl); ?>
一、先确认URL是否存在问题
打印完整URL并手动测试
在构建完$url后,立即添加代码打印URL并终止脚本,把生成的URL复制到浏览器/Postman直接访问,看返回内容:// 构建URL后添加以下代码 echo $url; exit;如果手动访问也无有效返回,说明要么参数错误,要么API本身未返回数据。
检查参数名大小写
API文档示例中的参数是sqlquery(全小写),但你代码里用的是SQLquery(大小写混合),多数API对参数名大小写敏感,建议改成&sqlquery=再测试。
二、强化调试,获取完整请求/响应细节
导出cURL Verbose日志
你已经开启了CURLOPT_VERBOSE,但需要把日志输出到文件才能查看详情:$verboseLog = fopen('curl_debug.log', 'w+'); curl_setopt($curl, CURLOPT_STDERR, $verboseLog);执行脚本后打开
curl_debug.log,可查看完整请求头、响应头,确认API是否真的返回了内容。查看响应的真实内容
用var_dump($response)代替echo $response,即使返回空字符串或不可见字符,var_dump也会明确显示其类型和长度:// 替换原echo $response; var_dump($response);
三、其他可能的排查点
简化SQL语句测试
把SQL语句简化为Select RESOURCENAME from RESOURCEMASTER limit 1,排除复杂条件导致的API返回异常。验证format参数有效性
先将$format = 'xml';改成$format = 'json';,如果能拿到有效JSON返回,说明API可能不支持xml格式,或xml格式返回有问题。
内容的提问来源于stack exchange,提问作者Simon King

