fork(2) download
  1. <?php
  2. //Функция по URL возвращает строку с содержимым
  3. function get_content_from_source($url)
  4. {
  5. $parse = parse_url($url); //Разбирает URL и возвращает его компоненты
  6. //Формирует HTTP запрос
  7. $port = "80";
  8. $host = ($parse['host']);
  9. $page = ($parse['path']);
  10. $header = "GET $page HTTP/1.1\r\n";
  11. $header .= "Host: $host\r\n";
  12. $header .= "User-Agent: MySpider/1.0\r\n";
  13. $header .= "Accept: text/html, text/plain, application/xml\r\n";
  14. $header .= "Accept-Language: ru, ru-RU\r\n";
  15. $header .= "Accept-Charset: windows-1251\r\n";
  16. $header .= "Accept-Encoding: identity\r\n";
  17. $header .= "Connection: close\r\n";
  18. $header .= "Cookie: test=5\r\n";
  19. $header .= "\r\n";
  20.  
  21. $fsock = fsockopen($host, $port, $err, $err_text, 30); //Открывет соединение с интернет сокетом
  22. if ($fsock) {
  23. stream_set_blocking($fsock, 0); //Устанавливает неблокирующий режим на потоке
  24. fwrite($fsock, $header); //Пишет HTTP запрос в поток
  25. $s = 5; //Для проверки конца HTTP заголовка
  26. $content = ""; //Контент
  27. $headers = ""; //Заголовок HTTP ответа
  28. $buffer = ""; //Буфер
  29. while (!feof($fsock)) { //Проверяет, достигнут ли конец файла
  30. $buffer = fgets($fsock, 1024); //Читает строку из файла до 8Кбайт
  31. if ($buffer == "\r\n") {
  32. $s = 10;
  33. } //Если конец заголовка меняет $s
  34. if ($s == 5) {
  35. $headers .= $buffer;
  36. } //Если заголовок — пишем
  37. else {
  38. $content .= $buffer;
  39. } //Если контент — пишем
  40. }
  41. fclose($fsock); //Закрывает соединение с интернет сокетом
  42. } else {
  43. echo "Произошла ошибка " . $err . ": " . $err_text;
  44. return $content;
  45. }
  46. return $content;
  47. }
  48.  
  49. $url = "http://w...content-available-to-author-only...d.com/Matches/834416/LiveStatistics/International-FIFA-World-Cup-2014-Brazil-Chile";
  50. $content = get_content_from_source($url);
  51. $f = fopen('content.txt', 'w+');
  52. fwrite($f, $content);//Пишет в файл контент
  53. fclose($f);
  54. /*Далее пытаюсь парсит вот это var initialData = [[[409,418,'Brazil','Chile'.........]]];
  55. их несколько таблиц там*/
  56. $pattern = '#\s*var\s+initialData\s*=\s*(.+);#';
  57. preg_match_all($pattern, $content, $array);
  58.  
  59. print_r($array);//В браузер
  60.  
  61. $str = serialize($array);
  62. $fi = fopen('res.txt', 'w+');
  63. fwrite($fi, $str);//В файл
  64. fclose($fi);
Success #stdin #stdout #stderr 0.01s 20640KB
stdin
Standard input is empty
stdout
Произошла ошибка 0: php_network_getaddresses: getaddrinfo failed: Name or service not knownArray
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

)
stderr
PHP Warning:  fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/5clrgO/prog.php on line 21
PHP Warning:  fsockopen(): unable to connect to www.whoscored.com:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/5clrgO/prog.php on line 21
PHP Notice:  Undefined variable: content in /home/5clrgO/prog.php on line 44
PHP Warning:  fopen(content.txt): failed to open stream: Permission denied in /home/5clrgO/prog.php on line 51
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in /home/5clrgO/prog.php on line 52
PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in /home/5clrgO/prog.php on line 53
PHP Warning:  fopen(res.txt): failed to open stream: Permission denied in /home/5clrgO/prog.php on line 62
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in /home/5clrgO/prog.php on line 63
PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in /home/5clrgO/prog.php on line 64