/** * Performs POST-request * @param string $url * @param array $data * @param array $params */ public function post($url, $data = [], $params = null) { if (!is_array($params)) { $params = ['resultcb' => $params]; } if (!isset($params['uri']) || !isset($params['host'])) { $prepared = Pool::parseUrl($url); if (!$prepared) { if (isset($params['resultcb'])) { call_user_func($params['resultcb'], false); } return; } list($params['host'], $params['uri']) = $prepared; } if ($params['uri'] === '') { $params['uri'] = '/'; } $this->lastURL = 'http://' . $params['host'] . $params['uri']; if (!isset($params['version'])) { $params['version'] = '1.1'; } if (!isset($params['contentType'])) { $params['contentType'] = 'application/x-www-form-urlencoded'; } $this->writeln('POST ' . $params['uri'] . ' HTTP/' . $params['version']); if (isset($params['proxy'])) { if (isset($params['proxy']['auth'])) { $this->writeln('Proxy-Authorization: basic ' . base64_encode($params['proxy']['auth']['username'] . ':' . $params['proxy']['auth']['password'])); } } if (!isset($params['keepalive']) || !$params['keepalive']) { $this->writeln('Connection: close'); } $this->writeln('Host: ' . $params['host']); if ($this->pool->config->expose->value && !isset($params['headers']['User-Agent'])) { $this->writeln('User-Agent: phpDaemon/' . Daemon::$version); } if (isset($params['cookie']) && sizeof($params['cookie'])) { $this->writeln('Cookie: ' . http_build_query($params['cookie'], '', '; ')); } foreach ($data as $val) { if (is_object($val) && $val instanceof UploadFile) { $params['contentType'] = 'multipart/form-data'; } } $this->writeln('Content-Type: ' . $params['contentType']); if ($params['contentType'] === 'application/x-www-form-urlencoded') { $body = http_build_query($data, '', '&', PHP_QUERY_RFC3986); } elseif ($params['contentType'] === 'application/x-json') { $body = json_encode($data); } else { $body = 'unsupported Content-Type'; } $this->writeln('Content-Length: ' . strlen($body)); if (isset($params['headers'])) { $this->customRequestHeaders($params['headers']); } if (isset($params['rawHeaders']) && $params['rawHeaders']) { $this->rawHeaders = []; } $this->writeln(''); $this->write($body); $this->writeln(''); $this->onResponse($params['resultcb']); }
/** * Send request headers * @param $type * @param $url * @param &$params * @return void */ protected function sendRequestHeaders($type, $url, &$params) { if (!is_array($params)) { $params = ['resultcb' => $params]; } if (!isset($params['uri']) || !isset($params['host'])) { $prepared = Pool::parseUrl($url); if (!$prepared) { if (isset($params['resultcb'])) { $params['resultcb'](false); } return; } list($params['host'], $params['uri']) = $prepared; } if ($params['uri'] === '') { $params['uri'] = '/'; } $this->lastURL = 'http://' . $params['host'] . $params['uri']; if (!isset($params['version'])) { $params['version'] = '1.1'; } $this->writeln($type . ' ' . $params['uri'] . ' HTTP/' . $params['version']); if (isset($params['proxy'])) { if (isset($params['proxy']['auth'])) { $this->writeln('Proxy-Authorization: basic ' . base64_encode($params['proxy']['auth']['username'] . ':' . $params['proxy']['auth']['password'])); } } $this->writeln('Host: ' . $params['host']); if ($this->pool->config->expose->value && !isset($params['headers']['User-Agent'])) { $this->writeln('User-Agent: phpDaemon/' . Daemon::$version); } if (isset($params['cookie']) && sizeof($params['cookie'])) { $this->writeln('Cookie: ' . http_build_query($params['cookie'], '', '; ')); } if (isset($params['contentType'])) { if (!isset($params['headers'])) { $params['headers'] = []; } $params['headers']['Content-Type'] = $params['contentType']; } if (isset($params['headers'])) { $this->customRequestHeaders($params['headers']); } if (isset($params['rawHeaders']) && $params['rawHeaders']) { $this->rawHeaders = []; } if (isset($params['chunkcb']) && is_callable($params['chunkcb'])) { $this->chunkcb = $params['chunkcb']; } $this->writeln(''); $this->requests->push($type); $this->onResponse->push($params['resultcb']); $this->checkFree(); }