Я использую PHP curl для отправки серии запросов на сторонний сервер, для которого требуется вход в систему, а затем сохранение файла cookie сеанса для этого входа.
Поэтому я обернул операцию curl в этот класс:
class SoapCli {
private $ch;
private $id;
private $rc;
function __construct() {
$this->rc=0;
$this->id=bin2hex(random_bytes(8));
$this->ch = curl_init();
$time=microtime(true);
error_log(PHP_EOL.PHP_EOL."Instance id $this->id created ($time): \$this->ch = ".print_r($this->ch,true).PHP_EOL,3,"log.txt");
curl_setopt($this->ch, CURLOPT_AUTOREFERER,1);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, "");
curl_setopt($this->ch, CURLOPT_ENCODING, "");
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
}
function Request(string $method, string $url, array $headers = array(), $postdata = "", $referer = null) {
$resp = new stdClass();
$resp->id = $this->id;
$this->rc++;
$time=microtime(true);
error_log("Instance id $this->id before request $this->rc ($time): \$this->ch = ".print_r($this->ch,true).PHP_EOL,3,"log.txt");
try {
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
if (isset($referer)) curl_setopt($this->ch, CURLOPT_REFERER, $referer);
if (preg_match("/^POST$/i",$method)===1) curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postdata);
$resp->body = curl_exec($this->ch);
$resp->err_message = curl_error($this->ch);
$resp->err_number = curl_errno($this->ch);
$resp->info = curl_getinfo($this->ch);
}
catch (Exception $exception) {
$resp->err_message = $exception->getMessage();
$resp->err_number = $exception->getCode();
$resp->info = $exception->getTrace();
}
$time=microtime(true);
error_log("Instance id $this->id before request $this->rc ($time): \$this->ch = ".print_r($this->ch,true).PHP_EOL,3,"log.txt");
return $resp;
}
}
Однако после третьего запроса содержимое защищенной переменной, в которой хранится ресурс дескриптора завитка, заменено значением 0 (целое число), и я действительно не могу понять, почему. Я смог собрать только этот лог:
Instance id 1cb893bc5b7369bd created (1547852391.7976): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 1 (1547852391.8025): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 1 (1547852392.0723): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 2 (1547852392.0778): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 2 (1547852392.357): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 3 (1547852392.3616): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 3 (1547852392.6225): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 4 (1547852393.0264): $this->ch = 0
Instance id 1cb893bc5b7369bd before request 4 (1547852393.0758): $this->ch = 0
Instance id 1cb893bc5b7369bd before request 5 (1547852394.8992): $this->ch = 0
Instance id 1cb893bc5b7369bd before request 5 (1547852394.9461): $this->ch = 0
EDIT: Это код, который использует класс SoapCli
:
// index.php
$postdata = filter_input_array(INPUT_POST);
if ($_SESSION["logged_in"]===true) {
echo file_get_contents("main.html");
} else if (isset($postdata) && isset($postdata["action"])) {
$action = $postdata["action"];
if ($action==="Login" && isset($postdata["usrcpf"]) && isset($postdata["usrpwd"])) {
$username=$postdata["username"];
$password=$postdata["password"];
$sc=new SoapCli(); //instantiated here
$_SESSION["sc"]=$sc;
$login_response = $sc->Request(
"GET",
BASEURL."/login",
array(
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
"Cache-Control: max-age=0"
)
);
if ($login_response->err_number) {
echo file_get_contents("login_server_error.html");
} else {
$dom = new DOMDocument;
$dom->loadHTML($login_response->body);
$xdom = new DOMXPath($dom);
$csrf_token_nodes = $xdom->query("//input[@name='_csrf_token']/@value");
if ($csrf_token_nodes->length<1) {
echo file_get_contents("login_server_error.html");
} else {
$csrf_token = $csrf_token_nodes->item(0)->textContent;
$postdata = "_csrf_token=$csrf_token&_username=$username&_password=$password&_submit=Login";
$login_check_response = $sc->Request(
"POST",
BASEURL."/login_check",
array(
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
"Content-Type: application/x-www-form-urlencoded",
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1"
),
$postdata,
BASEURL."/login"
);
if ($login_check_response->err_number) {
echo file_get_contents("login_server_error.html");
} elseif (strpos($login_check_response->body, "api.js")) {
echo file_get_contents("login_auth_error.html");
} else {
$route_userinfo = $sc->Request(
"POST",
BASEURL."/route",
array(
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
"Accept: */*",
"Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
"Content-Type: application/json",
"X-Requested-With: XMLHttpRequest",
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
),
USERINFO_JSON,
BASEURL."/"
);
if ($route_userinfo->err_number) {
echo file_get_contents("login_server_error.html");
} else {
$_SESSION["logged_in"]=true;
$_SESSION["user_info"]=json_decode($route_userinfo->body);
header("Location: ".$_SERVER["PHP_SELF"], true, 303);
}
}
}
}
} else {
http_response_code(400);
}
} else {
echo file_get_contents("login.html");
}
а также
// ajax.php (called by JS in main.html, which is loaded after login)
if ($_SESSION["logged_in"]===true) {
$postdata = filter_input_array(INPUT_POST);
if (isset($postdata)) {
if (isset($postdata["content"])) {
if ($postdata["content"]==="tasks") {
$sc=$_SESSION["sc"];
$route_tasks = $sc->Request(
"POST",
BASEURL."/route",
array(
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
"Accept: */*",
"Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
"Content-Type: application/json",
"X-Requested-With: XMLHttpRequest",
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
),
TAKS_JSON,
BASEURL."/"
);
if ($route_tasks->err_number) {
echo file_get_contents("ajax_server_error.html");
} else {
$tarefas=json_decode($route_tasks->body);
if (isset($tarefas) && is_array($tarefas->records)) {
foreach($tarefas->records as $i=>$tarefa){
echo "<p>".$tarefa->especieTarefa->nome."</p>";
}
} else {
http_response_code(500);
}
}
}
} else {
http_response_code(400);
}
} else {
http_response_code(400);
}
} else {
http_response_code(403);
}
Поскольку переменная SoapCli::ch
недоступна вне класса, я действительно не понимаю, как ее содержимое может быть изменено без оператора. Я не нашел никакой информации о HTTP-запросе/ответе, который также уничтожил бы дескриптор.
Дополнительная информация
Что бы это ни было, это не имеет отношения к запросу, потому что я пытался повторить запрос № 3, который является действительным и получает действительный ответ, и его повторение не удается, потому что дескриптор исчез.
Кроме того, то, что я пытаюсь реализовать в PHP, уже сделано полнофункциональным настольным приложением .NET (winforms), поэтому это не похоже на то, что это невозможно сделать по внешним причинам. Я просто пытаюсь сделать с PHP curl то же, что и с System.Net.HttpWebRequest
, и наткнулся на проблему, описанную в этом посте.
Как я могу сохранить ручку, пока она мне нужна?
Я использую PHP 7.2 в IIS Express/Windows 10.
New SoapCli instance
, но в сообщении журнала указано толькоcreated
. - person Barmar   schedule 19.01.2019SoapCli
? - person Barmar   schedule 19.01.2019CURLOPT_SSL_VERIFYPEER
- person Dharman   schedule 19.01.2019curl_setopt($this->ch, CURLOPT_FAILONERROR, true);
и вывестиcurl_getinfo($this->ch, CURLINFO_HTTP_CODE);
, возможно, curl закрывается после сбоя. Я не уверен, что блок try catch действительно работает с curl - person Solrac   schedule 19.01.2019$this->ch
? - person Barmar   schedule 19.01.20190 == false
правда, черт возьми, я думаю, что дажеnull == false
правда - person Solrac   schedule 19.01.2019var_dump()
илиvar_export()
вместоprint_r()
. - person Solrac   schedule 19.01.2019curl_setopt($this->ch, CURLOPT_FAILONERROR, true);
, и если я заменюprint_r
наvar_export
, будет печататься$this->ch = NULL
до запроса № 3 и$this->ch = 0
после этого. - person VBobCat   schedule 19.01.2019http 200
за каждый вызов, у которого есть ресурс? - person Solrac   schedule 19.01.2019try
блок идет вcatch
? - person miken32   schedule 19.01.2019GET /login' (receives text/html); 2.
POST /login_check` (отправляет application/x-www-form-urlencoded имя пользователя и пароль, получает text/html); 3.POST /route
(отправляет параметры приложения/json, получает tex/html); 4.POST /route
(отправляет параметры приложения/json, получает tex/html); и т. д. #3 и #4 похожи, но ручка исчезает после успешного #3. Что касается блокаtry
, я хотел поймать, что происходит, но ничего не вышло. - person VBobCat   schedule 19.01.2019