public function sendRequest($url, array $query_data = [], $cookie = true) { /** * @var \HttpQueryString $params */ $queryData = new \http\QueryString($query_data); /** * @var \HttpRequest $http */ $request = new \http\Client\Request("GET", $url); if (!empty($queryData)) { $request->getBody()->append($queryData); } $request->setContentType("application/x-www-form-urlencoded"); $client = new \http\Client(); $cookie_file = 'cookie.log'; if ($cookie) { $cookie_str = file_exists($cookie_file) ? file_get_contents($cookie_file) : ''; $cookie = new Cookie($cookie_str); $client_cookie = $cookie->getCookie('frontend'); if ($client_cookie != 'deleted') { $client->addCookies(['frontend' => $client_cookie]); } } $client->enqueue($request); $client->send(); /** @var \HttpResponse $response */ $response = $client->getResponse($request); printf("Sent:\n%s\n\n", $response->getParentMessage()); printf("%s returned '%s'\n%s\n", $response->getTransferInfo("effective_url"), $response->getInfo(), $response->getBody()); file_put_contents($cookie_file, ''); foreach ($response->getCookies() as $cookie) { /* @var $cookie http\Cookie */ foreach ($cookie->getCookies() as $name => $value) { $cookie = new \http\Cookie(); $cookie->addCookie($name, $value); file_put_contents($cookie_file, $cookie->toString(), FILE_APPEND); } } print_r($response->getHeaders()); return json_decode($response->getBody(), true); }
public function setContractId($contract_id) { // Ensure we're logged in and this contract ID is valid identifier for our case assert(!is_null($contract_id)); assert($this->isLoggedIn()); // Ignore switching to non-accessible contract if (!in_array($contract_id, $this->contracts)) { return false; } // Ignore calls for switching to current contract id) if ($this->contract_id == $contract_id) { return false; } // Do HTTP request with appropriate TLS version $request = new \http\Client\Request("POST", UCS::URL_SWITCH_CABINET, array('User-Agent' => UCS::USER_AGENT, 'Accept' => UCS::ACCEPT, 'Accept-Language' => UCS::ACCEPT_LANGUAGE, 'Host' => UCS::HOST, 'Cookie' => $this->getCookieHeader(), 'Upgrade-Insecure-Requests' => 1, 'Content-Type' => 'application/x-www-form-urlencoded', 'Origin' => UCS::ORIGIN, 'Referer' => UCS::URL_PROFILE)); $request->setQuery(array('change_ho' => 1)); $request->getBody()->append(new \http\QueryString(['hoid' => $contract_id])); $client = new \http\Client(); $client->setOptions(array("ssl" => ["version" => \http\Client\Curl\SSL_VERSION_TLSv1])); $client->enqueue($request)->send(); // Suppress HTML parser warnings $xml_error_mode = libxml_use_internal_errors(true); // Get Response cookies and text $re = $client->getResponse($request); $this->loadContractsSelection(null, $re->getBody()); // Reset xml_error_mode libxml_use_internal_errors($xml_error_mode); return true; }