Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified the usage by providing methods to easily prepare and send the post request #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions example/sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,7 @@
->addData('fed[]', 'def')
->addFile('file[]', __DIR__ . '/receiver.php')
->addFile('file[]', __DIR__ . '/sender.php')
->build();
->send("http://localhost/receiver.php");
} catch (FormDataBuilderException $e) {
die($e->getMessage());
}

$host = "localhost";
$path = "/receiver.php";
$fp = fsockopen($host, 8888);
if ($fp) {
$str = "POST " . $path . " HTTP/1.1\r\n";
$str .= "Host: " . $host . "\r\n";
$str .= "Content-Type: " . $formDataBuilder->getContentType() . "\r\n";
$str .= "Content-Length: " . strlen($data) . "\r\n";
$str .= "Connection: close\r\n\r\n";

$str .= $data;

fwrite($fp, $str);

$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);

$response = explode("\r\n\r\n", $result);
echo $response[1];
}
76 changes: 76 additions & 0 deletions src/MultipartFormDataBuilder/FormDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class FormDataBuilder

private array $data = [];
private array $files = [];
private array $headers = [];
private string $boundary;

public function __construct()
Expand All @@ -33,6 +34,20 @@ public function addFile(string $name, string $path): self
return $this;
}

/**
* @param string $header one valid http header as a single line string
*
* @throws FormDataBuilderException
*/
public function addHeader(string $header)
{
if(!$header){
throw new FormDataBuilderException('No header provided');
}

$this->headers[] = $header;
}

/**
* @return string
*/
Expand All @@ -46,6 +61,63 @@ public function getContentType(): string
return 'multipart/form-data; boundary=' . $this->boundary;
}


/**
* @param string $url Url to send the request to
*
* @return string
*
* @throws FormDataBuilderException
*/
public function send(string $url): string
{
$host = parse_url($url,PHP_URL_HOST);
$path = parse_url($url,PHP_URL_PATH);
$port = parse_url($url,PHP_URL_PORT);

if($port != ""){
$host .= ":" . $port;
}

if(!$host || !$path){
throw new FormDataBuilderException('Invalid url: '.$url);
}

$data = $this->build();

$fp = fsockopen($host, 8888);
if ($fp) {

$str = "POST " . $path . " HTTP/1.1\r\n";
$str .= "Host: " . $host . "\r\n";
foreach ($this->headers as $header) {
$str .= $header . "\r\n";
}
$str .= "Content-Type: " . $this->getContentType() . "\r\n";
$str .= "Content-Length: " . strlen($data) . "\r\n";
$str .= "Connection: close\r\n\r\n";

$str .= $data;

$success = fwrite($fp, $str);
if(!$success){
throw new FormDataBuilderException('Fail to send the request');
}

$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);

$response = explode("\r\n\r\n", $result);
return $response[1];
}
else{
throw new FormDataBuilderException('Unable to open socket to host: '.$host);
}
}

public function build(): string
{
$eol = "\r\n";
Expand All @@ -67,6 +139,10 @@ public function build(): string
$value = file_get_contents($filePath);
$fileContentType = finfo_file($finfo, $filePath);

if(!$fileContentType){
throw new FormDataBuilderException('Unable to identify the content type of one of the files: '.$filePath);
}

$data .= '--' . $this->boundary . $eol . 'Content-Disposition: form-data; name="' . $key . '"; '
. 'filename="'.basename($filePath).'"' . $eol
. 'Content-Type: ' . $fileContentType . $eol
Expand Down