Created
September 9, 2019 14:17
-
-
Save phpfour/9405074e3b60c03b0746d72a232a9bae to your computer and use it in GitHub Desktop.
HttpUtility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vroom\Notification\Utility; | |
final class HttpUtility | |
{ | |
public static function requestAsync($url, $params = [], $headers = [], $method = 'POST', $timeout = 30) | |
{ | |
$paramString = http_build_query($params); | |
$parts = parse_url($url); | |
$fp = fsockopen( | |
$parts['host'], | |
isset($parts['port']) ? $parts['port'] : $parts['scheme'] === 'https' ? 443 : 80, | |
$errno, | |
$errstr, | |
$timeout | |
); | |
// Data goes in the path for a GET request | |
if ($method === 'GET') { | |
$parts['path'] .= '?' . $paramString; | |
} | |
$out = sprintf("%s %s HTTP/1.1\r\n", $method, $parts['path']); | |
$out .= "Host: " . $parts['host'] . "\r\n"; | |
$out .= "Content-Type: application/x-www-form-urlencoded\r\n"; | |
foreach ($headers as $key => $value) { | |
$out .= sprintf("%s: %s\r\n", $key, $value); | |
} | |
if ($method === 'POST') { | |
$out .= "Content-Length: " . strlen($paramString) . "\r\n"; | |
} | |
$out .= "Connection: Close\r\n\r\n"; | |
// Data goes in the request body for a POST request | |
if ($method === 'POST' && isset($paramString) === true) { | |
$out .= $paramString; | |
} | |
fwrite($fp, $out); | |
fclose($fp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment