Skip to content

Instantly share code, notes, and snippets.

@nagiyevelchin
Last active October 7, 2023 11:15
Show Gist options
  • Save nagiyevelchin/9c3e30799844db32ad84e08b10ffc814 to your computer and use it in GitHub Desktop.
Save nagiyevelchin/9c3e30799844db32ad84e08b10ffc814 to your computer and use it in GitHub Desktop.
This PHP function is designed to send an HTTP POST request to a specified URL with given parameters, allowing you to customize headers and the referer. It uses the cURL library to handle the HTTP request and provides flexibility in customizing the request's headers and parameters. The function returns the response received from the POST request.
<?php
/**
* This function performs an HTTP POST request to a specified URL with given parameters.
*
* @param string $url The URL to which the POST request is sent.
* @param array $params An associative array containing the POST parameters as key-value pairs.
* @param string $referer The referer header for the request.
*
* @return mixed The response from the HTTP POST request.
*/
function httpPost($url, $params, $referer) {
$postData = '';
// Create name-value pairs separated by '&'
foreach ($params as $k => $v) {
$postData .= $k . '=' . $v . '&';
}
rtrim($postData, '&');
// Initialize a cURL session
if (!$ch = curl_init()) {
die("init problems");
}
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Uncomment this line if you want to include the response headers in the output
// curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Set custom HTTP headers
$headers[] = "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
$headers[] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[] = "Accept-Language:en-us,en;q=0.5";
$headers[] = "Accept-Encoding:gzip,deflate";
$headers[] = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$headers[] = "Keep-Alive:115";
$headers[] = "Connection:keep-alive";
$headers[] = "Cache-Control:max-age=0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Set the referer header
curl_setopt($ch, CURLOPT_REFERER, $referer);
// Execute the cURL session and store the response
$output = curl_exec($ch);
// Close the cURL session
curl_close($ch);
// Return the response from the HTTP POST request
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment