PHP fsockopen subfolder

563 views Asked by At

I have a page on my website that have a really long execution time, i solved the problem of the fact that it was too slow by flushing contents while writing, but now i need to load it from another website, if i had to do it from my website a include would have been enough, but since i need to load it from another website, i thought of using file_get_contents, the problem is that in this way the flushs don't work, and the page take a lot of time to load, i heard that the solution was using fsockopen and fread, so i tried to use some examples, and i get a thing like this

<?php
  ob_start();
  $fp = fsockopen("www.mysite.com", 80);
  if (!$fp) {
    echo "$errstr ($errno)<br />\n";
  } else {
    fwrite($fp, "Data sent by socket");
    $content = "";
    while (!feof($fp)) {
      $content .= fread($fp, 1024);
      ob_flush();
    }
  fclose($fp);
  echo $content;
  }
?>

The problem is that if i do this it work, but when i try something like

<?php
  ob_start();
  $fp = fsockopen("www.mysite.com/subfolder/index.php", 80);
  if (!$fp) {
    echo "$errstr ($errno)<br />\n";
  } else {
    fwrite($fp, "Data sent by socket");
    $content = "";
    while (!feof($fp)) {
      $content .= fread($fp, 1024);
      ob_flush();
    }
  fclose($fp);
  echo $content;
  }
?>

It says me something like:
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No such host is known. on line 2

What am I mistaking?

1

There are 1 answers

0
Celeste Capece On BEST ANSWER

UPDATE: Solved by myself: the correct code is

<?php 
  function HTTP_Post($URL) {
    ob_start(); 
    $URL_Info=parse_url($URL); 
    $fp = fsockopen($URL_Info["host"],80); 
    fwrite($fp, "GET ".$URL_Info["path"]." HTTP/1.0\r\n" );
    fwrite($fp, "Host: ".$URL_Info["host"]."\r\n");
    fwrite($fp, "Connection: Close\r\n\r\n");
    while(!feof($fp)) { 
      echo fgets($fp, 1024);
      ob_flush();
      flush();
    } 
    fclose($fp); 
  } 
  ini_set('max_execution_time', 300);
  HTTP_Post("http://www.corriere.it/cronache/"); 
?>