doubijiao2094
2014-11-22 15:56
浏览 60
已采纳

PHP fsockopen子文件夹

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 />
";
  } 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 />
";
  } 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?

图片转代码服务由CSDN问答提供 功能建议

我的网站上有一个页面,执行时间非常长,我解决了它的问题 写作时刷新内容太慢了,但现在我需要从另一个网站加载它,如果我必须从我的网站上做一个包含就足够了,但是因为我需要从另一个网站加载它,我想到了 使用file_get_contents,问题是以这种方式刷新不起作用,并且页面需要花费大量时间加载,我听说解决方案是使用fsockopen和fread,所以我试着使用一些例子,我得到 这样的事情

 &lt;?php 
 ob_start(); 
 $ fp = fsockopen(“www.mysite.com”,80); 
 if(n)  !$ fp){
 echo“$ errstr($ errno)&lt; br /&gt; 
”; 
} else {
 fwrite($ fp,“socket发送的数据”); 
 $ content =  “”; 
 while(!feof($ fp)){
 $ content。= fread($ fp,1024); 
 ob_flush(); 
} 
 fclose($ fp); 
 echo $ 内容; 
} 
?&gt; 
   
 
 

Th 问题是,如果我这样做它工作,但当我尝试类似

 &lt;?php 
 ob_start(); 
 $ fp = fsockopen(“www  .mysite.com / subfolder / index.php“,80); 
 if(!$ fp){
 echo”$ errstr($ errno)&lt; br /&gt; 
“; 
}其他{\  n fwrite($ fp,“socket发送的数据”); 
 $ content =“”; 
 while(!feof($ fp)){
 $ content。= fread($ fp,1024); 
  ob_flush(); 
} 
 fclose($ fp); 
 echo $ content; 
} 
?&gt; 
   
 
 

它告诉我一些事情 喜欢:
警告:fsockopen():php_network_getaddresses:getaddrinfo失败:没有这样的主机。 第2行

我错了什么?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjunao9348 2014-11-23 14:03
    已采纳

    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
    " );
        fwrite($fp, "Host: ".$URL_Info["host"]."
    ");
        fwrite($fp, "Connection: Close
    
    ");
        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/"); 
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题