<?php//The url you wish to send the POST request to$url = $file_name;
//The data you want to send via POST$fields = ['__VIEWSTATE ' => $state,'__EVENTVALIDATION' => $valid,'btnSubmit' => 'Submit'];
//url-ify the data for the POST$fields_string = http_build_query($fields);
//open connection$ch = curl_init();
//set the url, number of POST vars, POST datacurl_setopt($ch,CURLOPT_URL, $url);curl_setopt($ch,CURLOPT_POST, true);curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//So that curl_exec returns the contents of the cURL; rather than echoing itcurl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post$result = curl_exec($ch);echo $result;?>
// Initialize Guzzle client$client = new GuzzleHttp\Client();
// Create a POST request$response = $client->request('POST','http://example.org/',['form_params' => ['key1' => 'value1','key2' => 'value2']]);
// Parse the response object, e.g. read the headers, body, etc.$headers = $response->getHeaders();$body = $response->getBody();
// Output headers and body for debugging purposesvar_dump($headers, $body);
class HTTPRequester {/*** @description Make HTTP-GET call* @param $url* @param array $params* @return HTTP-Response body or an empty string if the request fails or is empty*/public static function HTTPGet($url, array $params) {$query = http_build_query($params);$ch = curl_init($url.'?'.$query);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HEADER, false);$response = curl_exec($ch);curl_close($ch);return $response;}/*** @description Make HTTP-POST call* @param $url* @param array $params* @return HTTP-Response body or an empty string if the request fails or is empty*/public static function HTTPPost($url, array $params) {$query = http_build_query($params);$ch = curl_init();curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $query);$response = curl_exec($ch);curl_close($ch);return $response;}/*** @description Make HTTP-PUT call* @param $url* @param array $params* @return HTTP-Response body or an empty string if the request fails or is empty*/public static function HTTPPut($url, array $params) {$query = \http_build_query($params);$ch = \curl_init();\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);\curl_setopt($ch, \CURLOPT_HEADER, false);\curl_setopt($ch, \CURLOPT_URL, $url);\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');\curl_setopt($ch, \CURLOPT_POSTFIELDS, $query);$response = \curl_exec($ch);\curl_close($ch);return $response;}/*** @category Make HTTP-DELETE call* @param $url* @param array $params* @return HTTP-Response body or an empty string if the request fails or is empty*/public static function HTTPDelete($url, array $params) {$query = \http_build_query($params);$ch = \curl_init();\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);\curl_setopt($ch, \CURLOPT_HEADER, false);\curl_setopt($ch, \CURLOPT_URL, $url);\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');\curl_setopt($ch, \CURLOPT_POSTFIELDS, $query);$response = \curl_exec($ch);\curl_close($ch);return $response;}}
const FORMAT_CONTENT_LENGTH = 'Content-Length: %d';const FORMAT_CONTENT_TYPE = 'Content-Type: %s';
const CONTENT_TYPE_JSON = 'application/json';/*** @description Make a HTTP-POST JSON call* @param string $url* @param array $params* @return bool|string HTTP-Response body or an empty string if the request fails or is empty*/function HTTPJSONPost(string $url, array $params){$content = json_encode($params);$response = file_get_contents($url, false, // do not use_include_pathstream_context_create(['http' => ['method' => 'POST','header' => [ // header array does not need '\r\n'sprintf(FORMAT_CONTENT_TYPE, CONTENT_TYPE_JSON),sprintf(FORMAT_CONTENT_LENGTH, strlen($content)),],'content' => $content]])); // no maxlength/offsetif ($response === false) {return json_encode(['error' => 'Failed to get contents...']);}
return $response;}