Соединение M/Monit PHP CurlPhp

Кемеровские программисты php общаются здесь
Anonymous
Соединение M/Monit PHP Curl

Сообщение Anonymous »

Я пытаюсь написать PHP-скрипт для использования M/Monit API
https://mmonit.com/documentation/http-a ... /cURL.html< /p>
У меня возникла проблема с преобразованием сценария командной строки для запуска с использованием PHP 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
Вот что у меня есть

Код: Выделить всё

$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);
Я не получаю никаких данных, только форма входа возвращается каждый раз.
Я пробовал включать и выключать POST на шаге 3.
Правильно ли я использую Curl?
Кроме того, файл cookie в /tmp/cookies.txt создается правильно

Подробнее здесь: https://stackoverflow.com/questions/759 ... connection

Вернуться в «Php»