You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.9 KiB
76 lines
2.9 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace tts\services\http_requests;
|
|
|
|
final class http_socket_request {
|
|
|
|
public function http_request($options) {
|
|
$ret = '';
|
|
$verb = strtoupper( $options->get_verb() );
|
|
$cookie_str = '';
|
|
$sign = \bs_tts\common::is_string_found($options->get_uri(), '?') ? '&' : '?';
|
|
$getdata_str = count( $options->get_get_data() ) ? $sign : '';
|
|
$postdata_str = '';
|
|
|
|
foreach ($options->get_get_data() as $k => $v) {
|
|
$getdata_str .= urlencode($k) . '=' . urlencode($v) . '&';
|
|
}
|
|
foreach ($options->get_post_data() as $k => $v) {
|
|
$postdata_str .= urlencode($k) . '=' . urlencode($v) . '&';
|
|
}
|
|
foreach ($options->get_cookie_data() as $k => $v) {
|
|
$cookie_str .= urlencode($k) . '=' . urlencode($v) . '; ';
|
|
}
|
|
$crlf = "\r\n";
|
|
$req = $verb . ' ' . $options->get_uri() . $getdata_str . ' HTTP/1.1' . $crlf;
|
|
$req .= 'Host: ' . $options->get_host() . $crlf;
|
|
|
|
if ( $options->get_authentication() ) {
|
|
$req .= 'Authorization: Basic ' . base64_encode( $options->get_auth_name() . ':' . $options->get_auth_password() ) . $crlf;
|
|
}
|
|
|
|
$req .= 'User-Agent: ' . $options->get_user_agent() . $crlf;
|
|
$req .= 'Accept: ' . $options->get_app_type() . $crlf;
|
|
$req .= 'Accept-Language: ' . $options->get_language() . $crlf;
|
|
$req .= 'Accept-Encoding: ' . $options->get_encoding() . $crlf;
|
|
$req .= 'Accept-Charset: ' . $options->get_charset() . $crlf;
|
|
|
|
foreach ($options->get_custom_headers() as $k => $v) {
|
|
$req .= $k . ': ' . $v . $crlf;
|
|
}
|
|
if (!empty($cookie_str)) {
|
|
$req .= 'Cookie: ' . substr($cookie_str, 0, -2) . $crlf;
|
|
}
|
|
if ($verb == 'POST' && !empty($postdata_str)) {
|
|
$postdata_str = substr($postdata_str, 0, -1);
|
|
$req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf;
|
|
$req .= 'Content-Length: ' . strlen($postdata_str) . $crlf . $crlf;
|
|
$req .= $postdata_str;
|
|
} else {
|
|
$req .= $crlf;
|
|
}
|
|
if ( $options->get_include_http_response_headers() ) {
|
|
$ret .= $req;
|
|
}
|
|
if (($fp = @fsockopen($options->get_host(), $options->get_port(), $errno, $errstr)) == false) {
|
|
throw new \Exception( "Error {$errno}: {$errstr}\n" );
|
|
}
|
|
stream_set_timeout($fp, 0, $options->get_time_out() * 1000);
|
|
|
|
fputs($fp, $req);
|
|
while ($line = fgets($fp)) {
|
|
$ret .= $line;
|
|
}
|
|
fclose($fp);
|
|
|
|
$ret = \tts\misc::abort_on_crlf($ret);
|
|
|
|
if (! $options->get_include_http_response_headers() ) {
|
|
$ret = substr($ret, strpos($ret, "\r\n\r\n") + 4);
|
|
}
|
|
return ( $options->get_json_decode() ) ? json_decode($ret, true) : $ret;
|
|
}
|
|
|
|
}
|
|
|