Skip to content

Instantly share code, notes, and snippets.

@louismullie
Created February 7, 2012 23:57
Show Gist options
  • Save louismullie/1763153 to your computer and use it in GitHub Desktop.
Save louismullie/1763153 to your computer and use it in GitHub Desktop.
HTTP Get/Post Send
<?php
function send($host, $method, $path, $data, $useragent = null)
{
$str_data = '';
foreach ($data as $key => $value)
{
if ($str_data != '') $str_data .= '&';
$str_data .= rawurlencode($key) . '=' . rawurlencode($value);
}
// Supply a default method of GET if the one passed was empty
if (empty($method))
$method = 'GET';
$method = strtoupper($method);
$fp = fsockopen($host, 80);
if (!$fp) return false;
if ($method == 'GET')
$path .= '?' . $str_data;
fputs($fp, "$method $path HTTP/1.1\n");
fputs($fp, "Host: $host\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
if ($method == 'POST')
fputs($fp, "Content-length: " . strlen($str_data) . "\n");
if ($useragent == null)
fputs($fp, "User-Agent: MSIE\n");
else
fputs($fp, "User-Agent: $useragent\n");
fputs($fp, "Connection: close\n\n");
if ($method == 'POST')
fputs($fp, $str_data);
$buf = '';
while (!feof($fp))
$buf .= fgets($fp, 128);
fclose($fp);
return strstr($buf, '<');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment