Help. Отправка post запросов на php.

12
J
На сайте с 02.02.2009
Offline
53
#11
dvaes:
че-т не понял. к чему это в данной теме?)

TC же не указал конкретику :D

dvaes
На сайте с 03.09.2007
Offline
65
#12

я как-то спамил кое-чего (в хорошем смысле=) ) и там написал такое. можно модернизировать или так оставить


//получение страницы
function get_page($host, $page="/", $cookie="", $post="")
{
if (function_exists("curl_init"))
return curl_get_page($host, $page, $cookie, $post);

return socket_get_page($host, $page, $cookie, $post);
}
//через курл
function curl_get_page($host, $page="/", $cookie="", $post="")
{
$ch = curl_init ("http://" . $host . $page);

if ($post!="")
{
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
}
curl_setopt ($ch, CURLOPT_FAILONERROR, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_HEADER, 1);
//curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);

if ($cookie!="")
curl_setopt ($ch, CURLOPT_COOKIE, $cookie);

if (!$content = curl_exec ($ch))
return false;
curl_close ($ch);

list ($header, $html) = explode("\r\n\r\n", $content, 2);
return array($header, $html);
}
//загрузка контента при помощи сокета
function socket_get_page($host, $page="/", $cookie="", $post="")
{
if (!$fp = fsockopen ($host, 80))
return false;

socket_set_blocking ($fp, false);
socket_set_timeout ($fp, 5);

$protocol = "GET";
$header = "";

if ($cookie!="")
$header = "Cookie: " . $cookie . "\r\n";

if ($post!="")
{
$protocol = "POST";
$header .= "Content-Type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($post)."\r\n";
}

$request = $protocol . " " . $page." HTTP/1.0\r\n".
"Host: ".$host."\r\n".
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n".
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2\r\n".
$header.
"\r\n" . $post;

fputs ($fp, $request);

$content = "";
while (!feof ($fp))
$content .= fgets ($fp, 1024);

fclose ($fp);

list ($header, $html) = explode("\r\n\r\n", $content, 2);

return array($header, $html);
}
12

Авторизуйтесь или зарегистрируйтесь, чтобы оставить комментарий