Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: Общие вопросы > Как отладить работу с сокетами на локальном компе?


Автор: Orient 1.12.2006, 14:35
Здравствуйте

Вот с какой проблемой столкнулся. Пробую данны пример - http://forum.vingrad.ru/vingrad-faq/index.html

адрес указываю на своем локальном компе 

Код


$host = /*"forum.vingrad.ru"; */ 'localhost/phpAdmin';
$path = /* "index.php"; */ 'index.php';
$port = 80;
echo http_get($host, $path, $port);




и вот что мне выводится:

Warning: fsockopen(): php_network_getaddresses: gethostbyname failed in j:\program files\apache group\apache\htdocs\wsocktest.php on line 5

Warning: fsockopen(): unable to connect to localhost/phpAdmin:80 in j:\program files\apache group\apache\htdocs\wsocktest.php on line 5

Notice: Undefined variable: error in j:\program files\apache group\apache\htdocs\wsocktest.php on line 19


Не подскажете ли чего у меня не так?

Спасибо

Автор: BobiKK 1.12.2006, 15:54
The second parameter, if set, is expected to be an associative array © php.net. И вообще, заглядываем туда. Функцию, случаем, не перепутали?

Автор: Orient 1.12.2006, 17:19
Да чего тут путать то. Вот полный код странички:

Код

    function http_get($host, $path, $port = "80") {
     $content = '';
     if ($socket = fsockopen($host, $port)) {
      fputs($socket, "GET $path HTTP/1.1\r\nHost: $host\r\nConnection: close;\r\n\r\n");
      if (preg_match("/^HTTP\/1\.1 (\d+) /", fgets($socket), $error)) {
       while (!feof($socket)) {
        if (!preg_match("/^(.+?):\s*(.*)$/", fgets($socket), $array)) break;
        $header[strtolower($array[1])] = rtrim($array[2]);
       }
       if (isset($header['content-length'])) for ($i = 0; $i < $header['content-length']; $i++) $content .= fgetc($socket);
       else while (!feof($socket)) $content .= fgetc($socket);
      }
      fclose($socket);
     }
     if (isset($header['transfer-encoding']) && $header['transfer-encoding'] == 'chunked') $content = decode_chunk($content);
     if (isset($header['content-encoding']) && $header['content-encoding'] == 'gzip') $content = gzinflate(substr($content, 10));
     if ($error[1] == '302' && isset($header['location'])) {
      $url = parse_url($header['location']);
      return http_get($url['host'], $url['path'].($url['query'] ? "?".$url['query'] : ""), $url['port'] ? $url['port'] : 80);
     }
     return $content;
    }
    
$host = /*"forum.vingrad.ru"; */ 'localhost/phpAdmin';
$path = /* "index.php"; */ 'index.php';


$port = 80;
echo http_get($host, $path, $port);


Автор: Orient 2.12.2006, 23:25
Люди! Помогите!!!!

Автор: vasac 3.12.2006, 12:44
Orient, где line 5? где line 19?

Автор: Orient 4.12.2006, 00:25
line 5 - if ($socket = fsockopen($host, $port)) {

line 19 - if ($error[1] == '302' && isset($header['location'])) {

Добавлено @ 00:31 
а вот если подключаюсь к 
$host = 'www.yandex.ru';
$path = 'index.html';

то получаю вот что:
Fatal error: Call to undefined function decode_chunk() in c:\program files\apache group\Apache\htdocs\wtest.php on line 17

Автор: Orient 4.12.2006, 11:25
Облазимши весь данный форум нашел пример кода, который таки заработал.
Для несчастных последователей привожу пример:

Код

<?php

function GetFile($url)
{
    $purl = parse_url($url);
    $host = $purl['host'];
    $page = '';
    $headers = '';

    if ($socket = fsockopen($host, 80, $errno, $errstr, 30)) {
        
        if (isset($purl['path'])) {
            $headers .= "GET ".$purl['path']." HTTP/1.0\r\n";
        } else {
            $headers .= "GET / HTTP/1.0\r\n";
        }
        
        $headers .= "Accept: */*\r\n";
        $headers .= "Accept-Language: ru\r\n";
        $headers .= "Referer: My Referer\r\n";
        $headers .= "Host: ".$purl['host']."\r\n";
        $headers .= "User-Agent: UserAgent/1.0\r\n";
        $headers .= "Connection: Close\r\n\r\n";

        fputs($socket, $headers);

        while (!feof($socket)) {
            $page .= fgets($socket);
        }
        fclose($socket);

        $page = substr($page, (strpos($page, "\r\n\r\n") + 4), strlen($page));

        return $page;

    } else {
        return FALSE;
    }
}

$string = GetFile( 'http://localhost/index.php' );


echo $string;


?>

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)