P
P
PO6OT2015-05-21 08:15:06
PHP
PO6OT, 2015-05-21 08:15:06

Why doesn't the PHP code for getting a page via HTTP work?

Why the code does not work (it only writes "Result:" and emptiness, see hosthack.tk/?server=hostinger.ru&num=1 and generally takes 12 seconds to load):

echo 'Result:<br><br>'."\n";

$request = "GET / HTTP/1.1\r\n";
$request .= "Host: ".$_GET[server]."\r\n";
$request .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n";
[email protected]($_GET[server], 80, $errno, $errstr);
$text=fgets($page);
$text=substr($text, 0, strripos($text, ' '));
$text=str_replace("\r\n", ' ', $text);
$text=str_replace("\n", ' ', $text);
$text=str_replace('<', ' <', $text);
$text=preg_replace(array("/<script(.*)<\/script>/", "/<title(.*)<\/title>/", "/<textarea(.*)<\/textarea>/", "/<style(.*)<\/style>/"), ' ', $text);
$text=strip_tags($text);
$text=trim(preg_replace("/ +/", ' ', $text));

if(strlen($text) > 999){
  $text=substr($text, 0, 999);
  $textipos=strripos($text, ' ');
  $text=substr($text, 0, $textipos);
  $text.=" ...";
}else
  echo $text;

Problem code:
$request = "GET / HTTP/1.1\r\n";
$request .= "Host: ".$_GET[server]."\r\n";
$request .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n";
$text=fgets(@fsockopen($_GET[server], 80, $errno, $errstr));

That is, if you write instead of it $text=file_get_contents('http://'.$_GET[server]);, then everything will work. Only to file_get_contents () some sites are negative and do not give their page. That's why I decided to take fsockopen().
If you comment out the line $text=fgets($page);, then the page loads at a natural fast speed, but the result does not change.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-05-21
@woonem

Why not use curl or ready made libraries like this: simplehtmldom.sourceforge.net ? Or not to prescribe conditions, they say, if file_get_contents did not work, then do it with sockets? The fact is that your user agent may not pass on the hosting, or connections may be cut using other filters.
Maybe fsockopen throws an error? You have suppression of errors through the dog, if you remove it, some warnings will fall in, right?
What's in $errno and $errstr?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question