M/Monit API的PHP Curl转换问题:登录后无法获取数据
调用M/Monit API的PHP Curl问题
我尝试编写PHP脚本调用M/Monit API,参考官方提供的命令行curl示例,但每次返回的都是登录表单,无法获取数据。已确认/tmp/cookies.txt文件生成正常,修改第三步的POST设置后问题依旧,想确认是否正确使用了Curl。
官方curl示例
curl -c ~/.mmonit/cookie \ http://127.0.0.1:8080/index.csp curl -b ~/.mmonit/cookie \ -d z_username=admin \ -d z_password=swordfish \ -d z_csrf_protection=off \ http://127.0.0.1:8080/z_security_check curl -b ~/.mmonit/cookie \ http://127.0.0.1:8080/api/1/status/hosts/list
我编写的PHP Curl代码
$url1 = "http://127.0.0.1:8080/index.csp"; $url2 = "http://127.0.0.1:8080/z_security_check"; $url3 = "http://127.0.0.1:8080/api/1/status/hosts/list"; $dataa["z_username"] = "admin"; $dataa["z_password"] = "swordfish"; $dataa["z_csrf_protection"] = "off"; $headers[] = 'Content-type: application/json'; $data = json_encode($dataa); //STEP 1 Set Cookie $ch = curl_init($url1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); $response1 = curl_exec($ch); curl_close($ch); print_r($response1); //Step 2 Send Auth $ch = curl_init($url2); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); $response2 = curl_exec($ch); curl_close($ch); print_r($response2); //Step 3 Get data $ch = curl_init($url3); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); $response3 = curl_exec($ch); curl_close($ch); print_r($response3);
问题分析与修正
你的代码存在几个关键问题,导致无法正确完成认证并获取数据:
- Content-Type设置错误:官方curl示例用
-d参数提交的是application/x-www-form-urlencoded格式的数据,但你设置了Content-type: application/json并转成JSON格式,M/Monit无法正确解析登录参数,导致认证失败。 - 第三步请求方式错误:官方示例中第三步是GET请求,但你用了POST还带上登录数据,完全不符合API要求,自然返回登录页面。
- CURLOPT_CUSTOMREQUEST冗余使用:提交登录时直接用
CURLOPT_POST即可,无需额外设置CURLOPT_CUSTOMREQUEST,避免干扰请求头。
修正后的代码
$url1 = "http://127.0.0.1:8080/index.csp"; $url2 = "http://127.0.0.1:8080/z_security_check"; $url3 = "http://127.0.0.1:8080/api/1/status/hosts/list"; // 保持表单格式数组,无需转JSON $data = [ "z_username" => "admin", "z_password" => "swordfish", "z_csrf_protection" => "off" ]; // STEP 1 获取初始Cookie $ch = curl_init($url1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); $response1 = curl_exec($ch); curl_close($ch); // print_r($response1); // 可选查看初始页面内容 // Step 2 提交认证请求 $ch = curl_init($url2); curl_setopt($ch, CURLOPT_POST, true); // 使用标准POST curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // 转成表单格式字符串 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 自动跟随认证跳转,确保会话状态正确 curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); $response2 = curl_exec($ch); curl_close($ch); // print_r($response2); // 可选查看认证后跳转页面 // Step 3 获取API数据(GET请求) $ch = curl_init($url3); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); $response3 = curl_exec($ch); curl_close($ch); print_r($response3); // 输出API返回的数据
额外提示
CURLOPT_FOLLOWLOCATION可自动跟随认证后的跳转,确保会话状态一致。- 初始GET请求无需设置
Content-Type,避免不必要的请求头干扰。 http_build_query()将数组转为表单格式字符串,和curl-d参数行为完全一致。
内容的提问来源于stack exchange,提问作者Liam
相关产品推荐
相关产品推荐

