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?

  • 写回答

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/"); 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果