Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created March 25, 2012 13:21
Show Gist options
  • Select an option

  • Save stefanocudini/2193740 to your computer and use it in GitHub Desktop.

Select an option

Save stefanocudini/2193740 to your computer and use it in GitHub Desktop.
http post without curl
<?
function http_post_requestSock($url, $pdata=null, $getHeaders=false)
{
$urlinfo = parse_url($url);
$port = isset($urlinfo["port"]) ? $urlinfo["port"] : 80;
$pdata = is_array($pdata) ? http_build_query($pdata) : $pdata;
$cookie = 'username=user; password=pass; path=/';
$reqH = "POST ".$urlinfo["path"]." HTTP/1.1\r\n".
"Host: ".$urlinfo["host"]."\r\n".
"User-Agent: ".UAGENT."\r\n".
"Accept: */*"."\r\n".
"Cookie: ".$cookie."\r\n".
"Content-length: ".strlen($pdata)."\r\n".
#"Connection: close\r\n".
"Content-type: application/x-www-form-urlencoded\r\n";
$req = $reqH."\r\n".$pdata;
$fp = fsockopen($urlinfo["host"], $port);
stream_set_timeout($fp, 0, TIMEOUT * 1000);
fputs($fp, $req);
$resp='';
while(!feof($fp))
$resp .= fgets($fp, 1024);
fclose($fp);
//////////////////
$info['header_size'] = strpos($resp, "\r\n\r\n") + 4;
$info['request_header'] = $reqH;
$info['download_content_length'] = strlen(next(explode("\r\n\r\n",$resp)));
$body = ''; $head = array();
$resRows = explode("\r\n",substr($resp,0, $info['header_size']));
foreach($resRows as $row)
$head[ current(explode(': ',$row)) ] = next(explode(': ',$row));
//split http head in headers key=>value
$reqRows = explode("\r\n", trim($info['request_header']));
if($info['download_content_length']>0)
$body = substr($resp, -$info['download_content_length']);
else
$body = next(explode("\r\n\r\n",$resp));
$resp = $getHeaders ? array($head, $body) : $body;
print(#"URL: ".$url."\n".
"REQUEST: ".implode("\n".
" ",$reqRows)."\n".
"POST: ".$pdata."\n\n".
"RESPONSE: ".implode("\n".
" ",$resRows));
return $resp;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment