PrestaShop 8.0.2 API提交表单时出现500错误求助
PrestaShop 8.0.2 API集成表单提交500错误解决方案
错误详情
500 POST /connexion3.php HTTP/1.0
AH01215: PHP Fatal error: Uncaught PrestaShopWebserviceException: HTTP XML response is not parsable: array (: /var/www/cgi-bin/cgi_wrapper/cgi_wrapper, referer: https://dev.raph.fun/
AH01215: 'message' => 'Start tag expected, '<' not found: /var/www/cgi-bin/cgi_wrapper/cgi_wrapper, referer: https://dev.raph.fun/
错误核心:PrestaShop Webservice请求未返回有效XML响应,大概率是API路径配置错误或服务器返回了非XML错误内容。
原始代码参考
HTML登录页面
<!DOCTYPE html> <html> <head> <title>Page client</title> </head> <body> <h1>Page client</h1> <form id="loginForm" action="https://raph.fun/connexion3.php" method="POST"> <label for="email">E-mail:</label> <input type="email" id="email" name="email" required><br><br> <label for="password">Mot de passe:</label> <input type="password" id="password" name="password" required><br><br> <input type="submit" value="Se connecter"> </form> </body> </html>
PHP处理脚本
<?php require_once('/var/www/vhosts/raph.fun/httpdocs/PSWebServiceLibrary.php'); $url = "https://raph.fun"; // PS shop domain without "/api" $key = "MY-API-KEY"; //replaced my API key for asking question $debug = false; //true; // get information from PrestaShop $webService = new PrestaShopWebservice($url, $key, $debug); $email = $_REQUEST['email']; //$password = '123456789'; $password = $_REQUEST['password']; $optUser = array( 'resource' => 'customers', 'filter[email]' => '[' . $email . ']', 'display' => '[id,email,lastname,firstname,passwd]' ); $resultUser = ($webService->get($optUser)); foreach ($resultUser->customers->customer as $info) { if (password_verify($password, $info->passwd) == true) { session_start(); $response = array(); $response['status'] = 'succes'; $response['message'] = "You did it!"; setcookie("userId", $info->id); header('Content-type: application/json'); echo json_encode($response); } else { $response = array(); $response['status'] = 'error'; $response['message'] = 'Wrong password'; header('Content-type: application/json'); echo json_encode($response); } } ?>
API返回的有效XML响应
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> <customers> <customer> <id> <![CDATA[ 15 ]]> </id> <passwd> <![CDATA[ $2y$10$VNHbe8mjOBeDbV1xnLQlX.8KZJWCMgkGQIurFq3QGBUrxHbPbOoyW ]]> </passwd> <lastname> <![CDATA[ test ]]> </lastname> <firstname> <![CDATA[ test ]]> </firstname> <email> <![CDATA[ test@gmail.com ]]> </email> </customer> </customers> </prestashop>
修复步骤及优化代码
1. 核心修复点
- 指定完整API路径:初始化Webservice时添加
/api后缀,确保请求指向正确的API端点 - 添加异常捕获:捕获Webservice异常,返回详细错误信息便于排查
- 安全优化:用
$_POST替代$_REQUEST,避免GET参数干扰 - 边界处理:处理用户不存在、单个用户(非数组)的情况
- 代码规范:修正拼写错误(
succes→success),添加Cookie有效期和路径
2. 修复后的PHP代码
<?php session_start(); // 移至脚本顶部,避免Headers Already Sent错误 require_once('/var/www/vhosts/raph.fun/httpdocs/PSWebServiceLibrary.php'); $url = "https://raph.fun"; $key = "MY-API-KEY"; $debug = true; // 开启调试模式,方便排查API请求问题 try { // 明确指定PrestaShop API完整路径 $webService = new PrestaShopWebservice($url . '/api', $key, $debug); // 仅接收POST参数,提升安全性 $email = $_POST['email'] ?? ''; $password = $_POST['password'] ?? ''; $optUser = array( 'resource' => 'customers', 'filter[email]' => '[' . urlencode($email) . ']', // URL编码邮箱,避免特殊字符干扰 'display' => '[id,email,lastname,firstname,passwd]' ); $resultUser = $webService->get($optUser); // 检查是否返回用户数据 if (!isset($resultUser->customers->customer)) { header('Content-type: application/json'); echo json_encode([ 'status' => 'error', 'message' => '用户不存在' ]); exit; } // 处理单个用户返回的对象(非数组)情况 $customers = is_array($resultUser->customers->customer) ? $resultUser->customers->customer : [$resultUser->customers->customer]; foreach ($customers as $info) { if (password_verify($password, $info->passwd)) { $response = array( 'status' => 'success', 'message' => "登录成功!" ); // 设置Cookie有效期为7天,路径为全站 setcookie("userId", $info->id, time() + 3600*24*7, "/"); header('Content-type: application/json'); echo json_encode($response); exit; // 匹配到正确用户后立即退出,避免无效循环 } } // 所有用户都匹配失败,返回密码错误 header('Content-type: application/json'); echo json_encode([ 'status' => 'error', 'message' => '密码错误' ]); } catch (PrestaShopWebserviceException $e) { // 捕获API请求异常,返回详细错误信息 header('Content-type: application/json'); echo json_encode([ 'status' => 'error', 'message' => 'API请求失败', 'detail' => $e->getMessage() ]); exit; } ?>
3. 额外排查建议
- 检查服务器配置,确保
/api路径未被CGI wrapper或其他规则拦截 - 验证API密钥权限,确保拥有
customers资源的读取权限 - 开启
$debug = true后,查看Webservice输出的请求详情,确认请求URL和参数是否正确
内容的提问来源于stack exchange,提问作者Raph67110
相关产品推荐
相关产品推荐

